# Terminating an Active Directory user

I'm writing an AD Termination script for work. I'm new to PowerShell and was wondering if multiple commands within PowerShell switch statement is okay to do:

# Switch statement to start here
$switchTitle = "Terminate User"$confirmMessage = "Are you sure that " + $fullName + " is the user that you want to terminate?"$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes",
"Starts the termination process for the selected user"

$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", "Cancels the termination process and, eventually, will prompt for another user selection"$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes,$no)

$result =$host.ui.PromptForChoice($switchTitle,$confirmMessage, $options, 0) switch ($result)
{
0 {"You have selected Yes. The Termination Script will now start."
Copy-Item -Path "Microsoft.PowerShell.Core\FileSystem::\\Path\To\UserFolder" 
-Destination "Microsoft.PowerShell.Core\FileSystem::\\path\To\TermFolder" -Recurse -Force
# TODO: Put delete command here for old profile
# TODO: Put Disable AD Object command here
}
1 {"You have selected No. Please choose another user."}
}


I'm also concerned with PowerShell styling/conventions so if I'm doing anything wrong, please let me know.

# Prompting for confirmation

The way you're doing the prompt is overly complicated. Consider this:

[CmdletBinding(SupportsShouldProcess=$true)] param()$switchTitle = "Terminate User"
$confirmMessage = "Are you sure that " +$fullName + " is the user that you want to terminate?"

if ($PSCmdlet.ShouldContinue($confirmMessage, $switchTitle)) { # She said yes! } else { # nope }  I sometimes wrap this up into a small function that allows for quicker usage if it's always for a single purpose within a script and I need it a lot: function Get-Confirmation { [CmdletBinding(SupportsShouldProcess=$true)]
param(
[Parameter(Mandatory=$true)] [String]$User
)

$confirmMessage = 'Are you sure that {0} is the user that you want to terminate?' -f$User

$PSCmdlet.ShouldContinue($confirmMessage, 'Terminate User?')
}


Then just use it like so:

# code that populates $fullName if (Get-Confirmation -User$fullName) {
# terminate
} else {
# don't terminate
}


# Referencing files

Copy-Item -Path "Microsoft.PowerShell.Core\FileSystem::\\Path\To\UserFolder"
-Destination "Microsoft.PowerShell.Core\FileSystem::\\path\To\TermFolder" -Recurse -Force


You don't have to use the full provider name to reference a file, even for a UNC path.

It would be fine to just do this:

Copy-Item -Path "\\Path\To\UserFolder" 
-Destination "\\path\To\TermFolder" -Recurse -Force

• That looks so much better. I'm just getting an error saying that "Cannot find the type for custom attribute 'CmldetBinding'. Make sure that the assembly that contains this type is loaded." Sep 11 '15 at 17:34
• @MFonner oh that was a typo, fixed now (should have been CmdletBinding not CmldetBinding`). Sep 11 '15 at 17:36
• Awesome. I should have noticed that myself. Thanks for the help, looks a lot more clean now. Sep 11 '15 at 17:39