Amazing script, this is for converting VHD's but it is an amazing reference, even build a GUI: http://www.ms4u.info/2012/06/create-sysprep-vhd-and-vhdx-for-windows.html (actual script http://gallery.technet.microsoft.com/scriptcenter/Convert-WindowsImageps1-0fe23a8f)
test.ps1
# Example of PowerShell Loop echo "" echo "Simple Foreach" echo "--------------" $NumArray = (1,2,3,4,5,6,7,8,9,10,11,12) Foreach ($Item in $NumArray) {$Item * 13} echo "" echo "Split Path Variable" echo "-------------------" $a = $env:path; $a.Split(";")
Set objShell = CreateObject("Wscript.Shell") objShell.Run("powershell.exe -noexit c:\scripts\test.ps1")
http://www.powershellpro.com/powershell-tutorial-introduction
Command Snippets
Get-Help Get-Help * Get-Help get-* Get-Command -verb get Get-Command -verb set Get-Command -commandType alias Get-ChildItem | Sort {$_.Name} Get-ChildItem -Name R* Get-ChildItem | Where-Object {$_.name -like “R*â€} Get-ChildItem -Path C:\ -Recurse | Where-Object {$_.LastWriteTime -gt "08/25/2007"} Get-EventLog System | Group-Object eventid | Sort-Object Count -descending Get-Process | ConvertTo-html Get-Process | ConvertTo-html | out-file "Processes.html" Get-Process | Format-Table | ConvertTo-Html | tee .\Process.html Invoke-Item Process.html Get-PSProviders Get-PSDrive cd Alias: dir cd HKLM: dir New-Item -Path C:\NewFolder -Type Directory New-Item -Path C:\NewFolder\NewFile.txt -Type File
Script Snippets
$strA = "Hello " $strB = "World!" $strC = $strA += $strB $strC $strA = "Go east young man!" $strB = $strA -replace "east", "west" $strB #Single and double quotes, double can have variables (called interpolation) echo "My current dir is $PWD" #Datatypes are normally automatic, to force use [type] [string]$strComputer = "MyFileServer01" [int]$x = 9 [decimal]$y = 9.9 $x++ $x-- #Arrays $strComputers = @("Server1", "Server2", "Server3") $strComputers $strComputers.Count $strComputers[0] #Concat arrays $x = @(1, 2, 3, 4, 5) $y = @(6, 7, 8, 9, 10) $z = $x + $y $z $z.count #will be 10 $dirs = $env:Path.Split(';') $dirs.Count # Hash Tables (Associative Arrays) $EmpNumbers = @{"John Doe" = 112233; "Dave Davis" = 223344; "Justine Smith" = 334455} $EmpNumbers["John Doe"] $EmpNumbers.Remove("Rose Jones") $EmpNumbers.Clear() #removes ALL records $x = Read-Host "Enter a number:" if ($x -eq 5) { Write-Host "Hello my name is Bob" } elseif ($x -eq 4) { Write-Host "Hello, my name is Sue" } elseif ($x -eq 2) { Write-Host "Hello, my name is Troy" } elseif ($x -gt 1) { Write-Host "Hello, my name is Mary" } else { "I have no idea what my name is?" } for ($i=1; $i -le 5; $i++) {Write-Host $i} while ($i -le 5) {Write-Host $i; $i++} #Functions Function GetOS($hostname) { $OS = Get-WmiObject -Class win32_OperatingSystem -namespace "root\CIMV2" -ComputerName $hostname return $OS.Name } GetOS('dyna-web2')
Get Processes in gridview
# PowerShell Out-GridView Example Clear-Host Get-Service | Out-GridView
Get Processes with certain fields, in gridview
# PowerShe ll Out-GridView in action Clear-Host Get-Process | select-Object -property name, workingSet, peakWorkingSet | Out-GridView
BIOS Info
$strComputer = “.†$colItems = get-wmiobject -class Win32_BIOS -namespace root\CIMV2 -comp $strComputer foreach ($objItem in $colItems) { write-host “BIOS Characteristics: †$objItem.BiosCharacteristics write-host “BIOS Version: †$objItem.BIOSVersion write-host “Build Number: †$objItem.BuildNumber write-host “Caption: †$objItem.Caption write-host “Code Set: †$objItem.CodeSet write-host “Current Language: †$objItem.CurrentLanguage write-host “Description: †$objItem.Description write-host “Identification Code: †$objItem.IdentificationCode write-host “Installable Languages: †$objItem.InstallableLanguages write-host “Installation Date: †$objItem.InstallDate write-host “Language Edition: †$objItem.LanguageEdition write-host “List Of Languages: †$objItem.ListOfLanguages write-host “Manufacturer: †$objItem.Manufacturer write-host “Name: †$objItem.Name write-host “Other Target Operating System: †$objItem.OtherTargetOS write-host “Primary BIOS: †$objItem.PrimaryBIOS write-host “Release Date: †$objItem.ReleaseDate write-host “Serial Number: †$objItem.SerialNumber write-host “SMBIOS BIOS Version: †$objItem.SMBIOSBIOSVersion write-host “SMBIOS Major Version: †$objItem.SMBIOSMajorVersion write-host “SMBIOS Minor Version: †$objItem.SMBIOSMinorVersion write-host “SMBIOS Present: †$objItem.SMBIOSPresent write-host “Software Element ID: †$objItem.SoftwareElementID write-host “Software Element State: †$objItem.SoftwareElementState write-host “Status: †$objItem.Status write-host “Target Operating System: †$objItem.TargetOperatingSystem write-host “Version: †$objItem.Version write-host }
#OS Information $strComputer = Read-Host "Enter a computer hostname" $OS = Get-WmiObject -Class win32_OperatingSystem -namespace "root\CIMV2" -ComputerName $strComputer $OS.Version #BIOS Information $strComputer = "." $bios = Get-WmiObject -class Win32_BIOS -namespace root\CIMV2 -comp $strComputer $bios | Get-Member $bios.SerialNumber #Processor Information $cpu = get-wmiobject -class "Win32_Processor" -namespace "root\CIMV2" -computername $strComputer #HD Information $HD = get-wmiobject -class "Win32_DiskDrive" -namespace "root\CIMV2" -computername $strComputer