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 different ways to automate the update of the help: PowerShell profile Scheduled Job GPO (in case you work in a domain) Profile

Scheduled Job

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 dangerous and you can have unexpected behaviors. NoProfile… Read More »

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

Best Practice: It is recommended to use single quotes if there are no variables or escape sequences in strings, otherwise, you should use double quotes. Single quotes

Double quotes

Explanation: Single quotes disable the expansion of variables and should be preferred if there is no variable or escape sequence inside strings to reduce unexpected results (ex: if the… Read More »

Powershell Best Practice #12: Avoid Out-Null cmdlet

Best Practice: It is recommended to avoid Out-Null when you need to suppress the output of some commands inside large loops (for, foreach, etc.) for example.

Explanation: It is recommended to assign to $null or [void] casting for better performance.

When you want to suppress the output of a command (example: when adding items to a… Read More »

Powershell Best Practice #11: Use Set-StrictMode in your dev scripts

Best Practice: It is recommended to use Set-StrictMode for your development scripts to identify code issues. 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.

Here we can see we get an error as… 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 #7: Use approved verb (not unapproved verb)

Best Practice: It is recommended to use approved verb for cmdlet, do not use unapproved verb. Explanation: PowerShell cmdlets use the syntax Verb-Noun. Example: Get–Service (“Get” is the verb, “Service” is the noun). By following the syntax convention Verb-Noun we can immediately know what is the purpose of the cmdlet. Get = Reads something Set = Writes something… Read More »

Powershell Best Practice #9: Use custom folding regions

Best Practice: It is recommended to use region statement to split your code into logical parts in your scripts.   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 Confirm parameters 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… Read More »