-
Notifications
You must be signed in to change notification settings - Fork 248
Expand file tree
/
Copy pathremove-user-and-share.ps1
More file actions
62 lines (57 loc) · 1.88 KB
/
remove-user-and-share.ps1
File metadata and controls
62 lines (57 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#Requires -RunAsAdministrator
#Requires -Modules Microsoft.PowerShell.LocalAccounts, SmbShare
Param(
[Parameter(Mandatory = $false, HelpMessage = "The name of the share")][string]$ShareName = "steeltoe_network_share",
[Parameter(Mandatory = $false, HelpMessage = "The path to the share. For example: 'c:\steeltoe_network_share'")][string]$SharePath = "c:\steeltoe_network_share",
[Parameter(Mandatory = $false, HelpMessage = "The name of the user")][string]$UserName = "shareWriteUser"
)
$ErrorActionPreference = "Stop"
if ($PSVersionTable.PSVersion.Major -lt 6)
{
Write-Output "Running in Windows PowerShell (version < 6)"
}
else
{
Write-Output "Running in PowerShell (Pwsh) 7+"
Add-Type -AssemblyName System.Management.Automation
Import-Module Microsoft.PowerShell.LocalAccounts -SkipEditionCheck
}
if (Get-SmbShare $ShareName -ErrorAction SilentlyContinue)
{
Remove-SmbShare -Name $ShareName
Write-Host "SMB share $ShareName removed."
}
else
{
Write-Host "SMB share $ShareName was not found."
}
if (Get-LocalUser -Name $UserName -ErrorAction SilentlyContinue)
{
if (Get-LocalGroupMember -Group "Users" -Member $UserName -ErrorAction SilentlyContinue)
{
Write-Host "Removing $UserName from local 'Users' group..."
Remove-LocalGroupMember -Group "Users" -Member $UserName
Write-Host "User removed from group."
}
else
{
Write-Host "User $UserName was not found in 'Users' group."
}
Write-Host "Removing local user $UserName..."
Remove-LocalUser -Name $UserName
Write-Host "User completely removed."
}
else
{
Write-Host "User $UserName was not found."
}
if (Get-Item -Path $SharePath -ErrorAction SilentlyContinue)
{
Write-Host "Removing $SharePath from disk..."
Remove-Item -Path $SharePath -Recurse
Write-Host "Directory completely removed."
}
else
{
Write-Host "$SharePath was not found."
}