Category Archives: Dont-do-that

Don’t do that #25: Manually add whitespaces for padding strings

Don’t do that: You want to add exactly 20 whitespaces on the left side of a string : $twentyWhitespaces = '                    test' The length of the variable $twentyWhitespaces is 25 – 5 (test) = 20 whitespaces Do that: The above code works well but it is definitely not practical to manually count the number of whitespaces to add and human errors can… Read More »

Don’t do that #24: Use Get-QADUser (Quest) to receive AllMemberOf of a user

Don’t do that: There is the possibility to to get all the groups (nested) a user is memberOf with the cmdlet “Get-QADuser” of Quest (now DELL). (Get-QADUser -Identity SamAccountName).AllMemberOf Do that: The above code works well, but it requires a 3rd party Snapin, there is a native one-liner way to receive “AllMemberOf” for a user. Get-ADGroup -LDAPFilter "(member:1.2.840.113556.1.4.1941:=$($distinguishedName))"

Don’t do that #23: Use Where-Object to search a user with filter (like)

Don’t do that: The following code performs a search for users inside an OU where the name contains “vip”. (Get-ADUser -SearchBase 'OU=myOU,DC=domain,DC=com' -Filter * | ?{$_.Name -match 'vip'}).Name Do that: The above code works, but I don’t like because there is no filter applied, it loads in memory useless objects. In this specific scenario, try to avoid the pipeline (filter before, not after).… Read More »

Don’t do that #21: Use multiple Replace to select numbers in a string

Don’t do that: I saw that many times, some people want to extract some numbers in a string and they use the Replace method several times.

or $log -replace 'backup-full-', '' -replace '-disk-D', '' It will return ‘22042015’ in this case, but I don’t like this approach because it is “too much” work and in case the prefix/suffix is changed,… Read More »

Don’t do that #20: Use too many parameters in one line (use splatting instead)

Don’t do that: Too many parameters in one line is hard to read and to debug:

Another “bad” way to is by using backticks (` escape character in Powershell) because it is hard to see and also because a whitespace after a backtick will return an error.

Do that: My favourite way is to use… Read More »

Don’t do that #19: Use double quotes to escape characters

Don’t do that: You want to display: The share “\\server\share$“ is not available (where share$ is a hidden share). You write the following code and you escape the double quotes with a backtick (`), the double quotes are missing: ““

Output: So you do the following and it works: Do that: A better way… Read More »

Don’t do that #18: Use a loop For to get the line number

Don’t do that: You want to return the line number of a specific string “user04”. File.txt


Do that: A faster method to return the line number is: (Select-String -Path .\test.txt -Pattern 'user04').LineNumber

Don’t do that #17: Silent the errors with $ErrorActionPreference = ‘SilentlyContinue’

Don’t do that: It’s not recommended to silent the errors messages modifying the preference variable $ErrorActionPreference: $ErrorActionPreference = 'SilentlyContinue' Do that: Avoid to modify the default value of $ErrorActionPreference. By default, the value is $ErrorActionPreference = 'Continue' First, because you could not be aware when an error occurs and second, it pollutes the user’s session by modifying the preference variable. Doing that, all the cmdlets are… Read More »

Don’t do that #16: Use the For loop to create an infinite loop

Don’t do that: The following code is creating an endless loop to ping a server.

Do that: Another way to create an endless loop (using a for loop with empty initialization, condition, and afterthought).

Note: The parameter -Quiet returns a boolean value (True/False).