0

Is it possible to configure a PowerShell session to warn me any time I run a specifically named command/function?

As an example (I'd like to get warnings every time I use a command that begins with "Set-"):

       If I run a Get-foo command, it runs that command without warning.
       If I run a Set-foo command, then it provides a warning about changes being made.
1
  • I'm not sure why this was down-voted (Perhaps the 'user responsibility' motivation?. Please help me understand why at least :) Commented Mar 12, 2018 at 18:08

1 Answer 1

4

You can use the $PSDefaultParameterValues variable to define defaults, and many Set- commands include a 'Confirm' parameter, or a 'WhatIf' parameter. You could do something like:

$PSDefaultParameterValues = @{
    "Set-*:Confirm"=$true
    "Set-*:WhatIf"=$true
}

The format for this is a hashtable, where the key is ":" and the value is the value that you want to set. Wildcards are permitted for cmdlet names, so you can apply the setting to all Set- cmdlets easily enough.

If you wanted to use 'WhatIf' you would simple have to run the command with -WhatIf:$false if you want the command to actually do things rather than just tell you what it would have done.

Get-Item C:\Temp\*.docx | Copy-Item -Dest "$home\desktop" -WhatIf:$false

See this page for more information about the topic.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.