Category Archives: Dont-do-that

Don’t do that #15: Use Add method when creating an array with multiple elements

Don’t do that: An empty array is created and then the elements are added.

Do that: There are several ways to do that with one-liners:

Don’t do that #14: Use Add method when creating a hashtable with multiple elements

Don’t do that: An empty hashtable is created and then the elements are added.

Do that: In this scenario, I prefer to create the hashtable with the elements (key/value), not after the creation: it’s faster and more practical.

Note: The attribute “ordered” is optional (it exists since Powershell v3 and creates an ordered dictionary).

Don’t do that #13: Use Write-Host to properly align the output

Don’t do that: The following code needs to properly align the output of these 3 variables:

Do that: A better way to output objects:

Don’t do that #12: Use several lines to get the parent container path of an AD user object

Don’t do that: The following code gets the parent container path of an AD user object. Example: CN=Powershell Test,OU=Users,OU=Department,DC=domain,DC=com $dn = (Get-ADUser -Identity $user -Properties DistinguishedName).DistinguishedName $split = $dn -split '(?<![\\]),' $split[1..$($split.Count-1)] -join ',' Do that: It can be done with a easier one-liner: $parent = $dn.split(',',2)[1]

Don’t do that #11: Reload a module with Remove-Module and Import-Module

Don’t do that: The following code remove and import a module in order to get the latest changes of a module. Indeed, when creating a module, we could need to reload the module into the current session to work with the latest changes (useful during development). Remove-Module -Name module Import-Module -Name .\module.psd1 Do that: It’s easier to reload the module with the… Read More »

Don’t do that #10: Use Exception.Message to catch an error in Catch block

Don’t do that: The following code catch the error with “Exception.Message”:

Do that: I prefer to use the exception error in the catch block. You can have multiple Catch block (each section handles a specific error). Check the exception error code (Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException) and then put that inside the catch block. The commands inside the try… Read More »

Don’t do that #9: Use Write-Host for verbose messages

Don’t do that: The following code use multiple Write-Host for verbose messages:

Do that: Write-Host should not be used for verboese message, Write-Verbose exists for this purpose. It’s better to use Write-Verbose because you can just turn on/off without the need to remove these lines “Write-Verbose”. In case you decide to use Write-Host for verbose… Read More »

Don’t do that #8: Get all properties instead of specific one

Don’t do that: The following command retrieves all the properties for a user although only the property “PasswordLastSet” is required: Get-ADUser -Identity $user -Properties * | Select-Object -Property PasswordLastSet Do that: It’s better and faster to retrieve only the property “PasswordLastSet” (we only need this one). With very large queries, the difference between “-Properties *” and “-Properties PasswordLastSet” can make a significant difference. (Get-ADUser -Identity $user -Properties PasswordLastSet).PasswordLastSet

Don’t do that #7: Multiple lines to create variables with the same prefix

Don’t do that: The following code requires multiple lines to create variables (with the same prefix “number”): $number1 = 0 $number2 = 0 $number3 = 0 $number4 = 0 $number5 = 0 Do that: It’s possible to create multiple variables in one line and initialize them with a specific value, 0 for example: 1..5 | ForEach-Object -Process {New-Variable -Name "number$_" -Value 0}

Don’t do that #6: Use Clear-Host to prevent the output

Don’t do that: The following code clean the screen to prevent the output (the index of the item added into the collection) generated by the Add method. $array = New-Object System.Collections.ArrayList $array.Add('Item1') Clear-Host Do that: In cases like this one, it’s better to use [void] or $null to prevent the output to be displayed. This code returns an output with “0” (the… Read More »