-
-
Save Natfan/c50fc34624add852192888c4ae33d3fe to your computer and use it in GitHub Desktop.
Create a PowerShell prompt using .NET classes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Remove-MyItem { | |
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory, Position = 1)] | |
[string] | |
$Path | |
) | |
$item = Get-Item -Path $Path | |
# Create prompt body | |
$Title = "Confirm" | |
$Message = "Are you sure you want to perform this action?`nPerforming the operation ""Remove File"" on target $($item.FullName)." | |
# Create answers | |
$Yes = [Management.Automation.Host.ChoiceDescription]::new("&Yes", "Continue with only the next step of the operation.") | |
$YesToAll = [Management.Automation.Host.ChoiceDescription]::new("Yes to &All","Continue with all the steps of the operation.") | |
$No = [Management.Automation.Host.ChoiceDescription]::new("&No", "Skip this operation and proceed with the next operation.") | |
$NoToAll = [Management.Automation.Host.ChoiceDescription]::new("No to Al&l", "Skip this operation and all subsequent operations.") | |
$Suspend = [Management.Automation.Host.ChoiceDescription]::new("&Suspend", "Pause the current pipeline and return to the command prompt. Type ""exit"" to resume the pipeline.") | |
# Create ChoiceDescription with answers | |
$Options = [Management.Automation.Host.ChoiceDescription[]]($Yes, $YesToAll, $No, $NoToAll, $suspend) | |
# Show prompt and save user's answer to variable | |
$Response = $Host.UI.PromptForChoice($Title, $Message, $Options, 0) | |
# Perform action based on answer | |
switch ($Response) { | |
0 { Remove-Item -Path $Item.FullName; break } # Yes | |
1 { Remove-Item -Path $Item.FullName; break } # Yes to All | |
2 { return; break } # No | |
3 { return; break } # No to ALL | |
4 { return; break } # Suspend | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment