Here’s one that came up at work, and I thought I would put here on the blog for co-workers and other IT friends who might just be getting into PowerShell.

So I have been working to encourage collagues at work to automate routine tasks, and build-up their PowerShell scripting skills. PS skills are essential for any IT worker, and should be leveraged by most roles in IT from Service Desk, to Technicians, right on up through sysadmins and engineers. But these days, the most contact my co-workers get with PowerShell is just basic CLI sessions to connect to our Office 365 tenant, running one-liners for user-administration, tenant configs, etc.

In pressing the team to devlop more scripting experience, for automating mundane tasks, I could see they were repeatedly trying to create test conditions, to “see what the code would do, first”. They were struggling a bit with the age-old fear of “breaking something” associated with automating something. I could tell some on my team were not yet aware of the -Confirm and -WhatIf switches. So I sent them this example, to introduce a little more confidence:

Get-ChildItem -Path C:\Temp\CSV_Files\ -File *.csv -Recurse | Remove-Item -WhatIf

In the above one-liner, you can see that -WhatIf allows you to quickly see what the Remove-Item action would have done, if they hadn’t provided the -WhatIf switch.
This allowed my team to quickly build confidence in testing. You needn’t feel like you are taking risks when tools like -WhatIf are available to you.

And it’s a similar story with the -Confirm switch:

Get-ChildItem -Path C:\Temp\CSV_Files\ -File *.csv -Recurse | Remove-Item -Confirm

PowerShell will pause before each and every action, interactively prompting you to confirm an action before moving to the very next line to execute. For the newbie this builds confidence.

Hope this was helpful to anyone beginning their PowerShell scripting journey!