Category Archives: Best Practices

Powershell Best Practice #16: Update the help

Best Practice: It is recommended to update the help on a regular basis. Explanation: You have to run Powershell as an administrator to update the help : Update-Help -Force You have differents ways to automate the update of the help: PowerShell profile Scheduled Job GPO (in case you work in a domain) Profile

Scheduled Job

To list… Read More »

Powershell Best Practice #15: Execute scripts with -NoProfile parameter

Best Practice: It is recommended to use -NoProfile parameter when executing scripts. powershell.exe -NoProfile File "C:\scripts\Get-DiskSpace.ps1" Explanation: When you run a script via scheduled task or via a shortcut, Powershell will first load profiles and then run the script. You never know what contains these profiles, so it can be dangereous and you can have unexpected behaviours. NoProfile… Read More »

Powershell Best Practice #13: Avoid double quotes in strings if not necessary

Best Practice: It is recommended to avoid double quotes if there are no variables or escape sequences in strings, prefer single quotes. Single quotes (literal string)

Double quotes (expanding string)

Explanation: Single quotes disable the expansion of variables and should be preferred if there is no variable or escape sequence inside strings.

Powershell Best Practice #12: Avoid Out-Null

Best Practice: It is recommended to avoid Out-Null, use $null or [void] instead.

Explanation: When you want to supress the output of a command (example: when adding items to a collection), using Out-Null is slower than [void] or $null.

Powershell Best Practice #11: Use Set-StrictMode

Best Practice: It is recommended to use Set-StrictMode.

Explanation: It is the equivalent of the well known “Option Explicit” in VBS and useful to avoid to enforce coding rules (undeclared variables, etc.) and prevent some common scripting errors. There are different cases when using undeclared variables can be dangerous because you can get unexpected… Read More »

Powershell Best Practice #10: Avoid empty Catch block

Best Practice: It is recommended to avoid empty empty Catch block in a try-catch-(finally) statement.

Explanation: Leaving an empty catch block is not considered a good practice because when an error occcurs in the try block, no action is performed on the error.

Powershell Best Practice #9: Use custom folding regions

Best Practice: It is recommended to use region statement to split your code into logicial parts (ex: variables, functions, etc.) Explanation: Since PowerShell v3, ISE supports code folding (feature of some editors/IDEs that allows the user to hide/display some “fold” sections). In english, the verb “fold” means: So, you can fold/unfold  by clicking the + / – sign on… Read More »

Powershell Best Practice #8: Use WhatIf and Confirm parameters

Best Practice: It is recommended to use the WhatIf and Confirm switch parameters when working with commands modifying the system state (Stop-Process, Set-ADUser, …). Explanation: The WhatIf and Confirmparameters are useful when you use them with commands that modify something. WhatIf : Only displays the objects that would be affected and what changes would be made to those objects (without… Read More »