Skip to content

Instantly share code, notes, and snippets.

View PrateekKumarSingh's full-sized avatar

Prateek Singh PrateekKumarSingh

View GitHub Profile
# Method 7 - From PSObject by iterating properties
# and adding key-value pairs
$Object = New-Object psobject -Property @{
a = 1
b = 2
}
$Hashtable = @{} # emtpy hashtable
# access the properties of [PSObject] and iterate them
# Method 6 - Using Dictionary PowerShell Type Accelarator
$Dictionary = [System.Collections.Generic.Dictionary[string,int]]::new()
$Dictionary.id = 101
$Dictionary.age = 29
# Method 5 - Using strongly typed Dictionary
$Dictionary = New-Object 'System.Collections.Generic.Dictionary[String,String]'
$Dictionary['First'] = 'Prateek'
$Dictionary['Last'] = 'Singh'
$Dictionary['Age'] = 29
$Hashtable = [System.Collections.Specialized.OrderedDictionary]::new()
$Hashtable['First'] = 'Prateek'
$Hashtable['Last'] = 'Singh'
$Hashtable['Age'] = 29
# Method 3 - Using HashTable PowerShell type accelerator
$Hashtable = [hashtable]::new()
$Hashtable['First'] = 'Prateek'
$Hashtable['Last'] = 'Singh'
$Hashtable['Age'] = 29
# Method 2 - Using .Net class for Hashtable
$Hashtable = New-Object System.Collections.Hashtable
$Hashtable['First'] = 'Prateek'
$Hashtable['Last'] = 'Singh'
$Hashtable['Age'] = 29
# Method 1 - Using @{ }
$Hashtable = @{
First = 'Prateek'
Last = 'Singh'
Age = 29
}
# Method 1 - Using @{ }
$Hashtable = @{
First = 'Prateek'
Last = 'Singh'
Age = 29
}
# Method 2 - Using .Net class for Hashtable
$Hashtable = New-Object System.Collections.Hashtable
$Hashtable['First'] = 'Prateek'
X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*
function global:prompt {
# Multiple Write-Host commands with color
$path = (Get-Culture).TextInfo.ToTitleCase($pwd)
Write-Host $path -foregroundcolor Cyan
Write-Host '>' -ForegroundColor Blue -NoNewline
return " "
}