Skip to content

Latest commit

 

History

History
55 lines (43 loc) · 1.23 KB

File metadata and controls

55 lines (43 loc) · 1.23 KB

16. Advanced Scripting with Powershell

PowerShell Scripting
  • Dot sourcing

  • CmdletBinding

    • Verbose Output
    • Parameter Checks
    • SupportsShouldProcess (-WhatIf and -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.ps1
PS C:\Users\Windows10-32> Show-AdvancedScript -FilePath .\1.txt
PS 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