Powershell – Useful snippits of code

I find myself using some pieces of code frequently. As I work, I often look back and copy something so that I can save time. I have long since lost the references online from which I may have learned – but I give full credit to those wonderful folks online who post stuff.. so here are some of my favorites.

#
Comparing strings
#
$ARR_ERSAPPLIST = $ARR_ERSAPPLIST | Sort-Object
$ARR_NPGAPPLIST = $ARR_NPGAPPLIST | Sort-Object
$ARR_APPLIST = Compare-Object $ARR_ERSAPPLIST -DifferenceObject $ARR_NPGAPPLIST -PassThru
$ARR_APPLIST2 = Compare-Object -ReferenceObject $ARR_NPGAPPLIST -DifferenceObject $ARR_ERSAPPLIST -PassThru
$ERSfromNPG = $ARR_ERSAPPLIST |% {if ($ARR_NPGAPPLIST -contains $_) { $_ }}
$NPGfromERS = $ARR_NPGAPPLIST |% {if ($ARR_ERSAPPLIST -contains $_) { $_ }}

#
create a hash table object
#
$myObject2 = New-Object BOB -TypeName PSObject
$myObject2 | Add-Member -type NoteProperty -name Name -Value "Doug_PC"
$myObject2 | Add-Member -type NoteProperty -name Manufacturer -Value "Dell"
$myObject2 | Add-Member -type NoteProperty -name ProcessorSpeed -Value "2.6 Ghz"
$myObject2 | Add-Member -type NoteProperty -name Memory -Value "131"
$masterlist += $myObject2


#
Yes No
#
YesNo
$YesNo = [System.Windows.Forms.MessageBox]::Show("No User Name was entered. The script will exit")
$a = new-object -comobject wscript.shell
$intAnswer = $a.popup("Do you want to delete these files?", `
0,"Delete Files",4)
If ($intAnswer -eq 6) {
$a.popup("You answered yes.")
} else {
$a.popup("You answered no.")
}