-
Dot sourcing
-
CmdletBinding
- Verbose Output
- Parameter Checks
- SupportsShouldProcess (
-WhatIfand-Confirm)
-
Show-AdvancedScript.ps1
function Show-AdvancedScript
{
[CmdletBinding( SupportsShouldProcess = $True)]
param(
[Parameter()]
$FilePath
)
Write-Verbose "Deleting $FilePath"
if ($PSCmdlet.ShouldProcess("$Filepath", "Deleting file permanently"))
{
Remove-Item $FilePath
}
}PS C:\Users\Windows10-32> C:\Users\Windows10-32\Desktop\Show-AdvancedScript.ps1PS C:\Users\Windows10-32> Show-AdvancedScript -FilePath .\1.txtPS C:\Users\Windows10-32> Show-AdvancedScript -FilePath .\2.txt -Verbose
VERBOSE: Deleting .\2.txt
VERBOSE: Performing the operation "Deleting file permanently" on target ".\2.txt".
PS C:\Users\Windows10-32>PS C:\Users\Windows10-32> Show-AdvancedScript -FilePath .\3.txt -WhatIf
What if: Performing the operation "Deleting file permanently" on target ".\3.txt".
PS C:\Users\Windows10-32>PS C:\Users\Windows10-32> Show-AdvancedScript -FilePath .\3.txt -Confirm