Category Archives: Best Practices

Best Practice: It is recommended to use the same name for your parameter (for your functions) than the native one parameter of the builtin Powershell cmdlets. Explanation: If you create a function which takes as a parameter a computerName, you should use the name : ComputerName Why “ComputerName”? Because many builtin cmdlets use this parameter… Read More »

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

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 »

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.

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 »

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 »

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 »