PS: Removing Backticks

1 minute read


So lately I have been going over my code and trying to remove backticks. That is because, although they work, they can cause a script to fail with a simple space character is placed after them. The way to do this has often been to place objects in hash tables instead. So now I follow this setup for hash tables and arrays:

To Resolve:

1a. Hashtables go in four steps – initialize, populate, call the function, and then clear the variable. You will see this often with my SetReg function for example:

$Params = @{}
$Params.Path= "HKCU:\Some\Path"
$Params.Name = "Category"
$Params.Value = "1"
$Params.Type = "DWORD"
SetReg @Params
Clear-Variable -Name Params

1a. To test a value

if( $Params.Value -eq 1 ){...}
#For Example from the hash table above:
If ($Params.Path -eq "HKCU:\Some\Path")
Write-Output "Path is correct

1b. To remove a key:

# For example:
# After this, $Params will still have the "Name", "Value", and "Type" properties - just not Path.
# For more reading, see my references below

2. Arrays:

# For short arrays (less than 100 items)
$Array = @()
ForEach ($Item in $Collection)
$Array += $Item

# For more than 100 items:
$Array = [System.Collections.ArrayList]@()
ForEach ($Item in $Collection)