Alex Rivera | Logout

how to use 'dry-run' in powershell

Asked 2012-06-26T16:10:15.373
18

I'm just a starter, dry-run is pretty useful to make a parameter to test, can any body tell me how to use it with an easy way? I googled that but few results on its uses.

Thank you very much

Edit
Report

2 Answers

34

Use on existing cmdlets

Explicitly supply the -WhatIf parameter:

rm foo.txt -WhatIf

Result:

What if: Performing operation "Remove File" on Target "C:\temp\foo.txt".

Use on existing cmdlets within a function or script

SupportsShouldProcess attribute automatically propagates -WhatIf to supported cmdlets:

function do-stuff
{
    [CmdletBinding(SupportsShouldProcess=$True)]
    param([string]$file)
    Remove-Item $file
}

do-stuff foo.txt -WhatIf

Result:

What if: Performing operation "Remove File" on Target "C:\temp\foo.txt".

Use on your own code blocks

Explicitly use ShouldProcess() method to determine whether -WhatIf was passed:

function do-stuff
{
    [CmdletBinding(SupportsShouldProcess=$True)]
    param([string]$file)
    if ($PSCmdlet.ShouldProcess($file)) {
        Write-Host "Deleting file"
        Remove-Item $file
    }
}

do-stuff foo.txt -WhatIf

Result:

What if: Performing operation "do-stuff" on Target "foo.txt".

Use on Nested functions in the same module

SupportsShouldProcess attribute automatically propagates -WhatIf to nested functions:

function do-stuff
{
    [CmdletBinding(SupportsShouldProcess=$True)]
    param([string]$file)
    if ($PSCmdlet.ShouldProcess($file)) {
        Write-Host "Deleting file"
        Remove-Item $file
    }
    inner "text"
}

function inner
{
    [CmdletBinding(SupportsShouldProcess=$True)]
    param([string]$s)
    if ($PSCmdlet.ShouldProcess($s)) {
        Write-Host "Inner task"
    }
    $s | out-file "temp9.txt"
}
do-stuff foo.txt -WhatIf

Result:

What if: Performing operation "do-stuff" on Target "foo.txt".
What if: Performing operation "inne
answered 2012-06-27T18:01:56.350
4

You are probably referring to the -WhatIf and -Confirm parameters on cmdlets. You can read up about them in Get-Help about_commonParameters:

Risk Management Parameter Descriptions

-WhatIf[:{$true | $false}]

Displays a message that describes the effect of the command, instead of executing the command.

WhatIf parameter overrides the value of the $WhatIfPreference variable for the current command. Because the default value of the $WhatIfPreference variable is 0 (disabled), WhatIf behavior is not performed without the WhatIf parameter. For more information, type the following command:

get-help about_preference_variables

Valid values:

$true (-WhatIf:$true). Has the same effect as -WhatIf.
$false (-WhatIf:$false). Suppresses the automatic WhatIf behavior that results when the value of the $WhatIfPreference variable is 1.

For example, the following command uses the WhatIf parameter in a Remove-Item command:

PS> remove-item date.csv -whatif

Instead of removing the item, Windows PowerShell lists the operations it would perform and the items that would be affected. This command produces the following output:

What if: Performing operation "Remove File" on
Target "C:\ps-test\date.csv".
-Confirm[:{$true | $false}]

Prompts you for confirmation before executing the command.

The Confirm parameter overrides the value of the $ConfirmPreference variable for the cur

answered 2012-06-26T16:26:34.273

Your Answer