Basic Tutorials [Link] [Link] Microsofts tips and ticks [Link]
[Link] Some cool tools This is VERY COOL: PowerShell Scriptomatic [Link] Useful Links I found a website the purports to have an impersonation script [Link] SQL Server specific scripts [Link] PowerShell cmdlet list [Link] Lots of scripts [Link] SendKeys in PowerShell [Link] mspx Much more complex SendKeys [Link] Pretty good SendKeys example [Link] SQL Server Links o Detailed backup with SQL Server [Link] o Lots of free SQL Server Scripts [Link]
Useful Commands get-acl Get the security descriptor for a resource, such as a file or registry key. Get-History Get a list of the previous commands entered during the current session Set-Date Change the system time on the computer to a time that you specify. get-credential start-service -name Fax (also stop-service and restart-service) get-Service | where {$_.Status -eq "Started"} get-EventLog system -newest 2000 | where {$_.entryType -match "Error"} get-psDrive - get drives and info about them. get-Process get all processes get-service Get-Service | Where-Object {$_.status -eq "stopped"} o Also {$_.status -eq "started"} get-WmiObject get-WmiObject win32_computersystem
get-process Get-Process | Where-Object {$_.PrivateMemorySize -gt 50MB} | SELECT ProcessName, PrivateMemorySize, CPU | Format-List
Clean up your XML output with -NoTypeInformation (Get-ChildItem C:\Scripts | ConvertTo-XML -NoTypeInformation) | Get-Member
Note: to run a script when you are in the same directory you need to .\scriptname.ps1 Useful Scripts Find OS Version $strCategory = "computer" $strOS = "Windows*Server*" $objDomain = New-Object [Link]
$objSearcher = New-Object [Link] $[Link] = $objDomain $[Link] = ("OperatingSystem=$strOS") $colProplist = "name" foreach ($i in $colPropList){$[Link]($i)} $colResults = $[Link]() foreach ($objResult in $colResults) { $objComputer = $[Link]; $[Link] } Update Windows #Only works with PowerShell 2.0
$FileReport = $true #put your own file here $FileReportPath = "c:\IT\Windows Update Reports\" $AutoRestart = $true $AutoRestartIfPending = $true $Path = $FileReportPath + "$env:ComputerName" + "_" + (Get-Date -Format dd-MMyyyy_HH-mm).ToString() + ".html" #Testing if there are any pending reboots from earlier Windows Update sessions
if (Test-Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired"){
#Report to file if enabled if ($FileReport -eq $true) { "Invoke-WindowsUpdate was run on $env:ComputerName, and the server $status `nPlease run Invoke-WindowsUpdate again when the server is rebooted." | Out-File FilePath $path } #Reboot if autorestart for pending updates is enabled if ($AutoRestartIfPending) {[Link] /t 0 /r } } exit } #Checking for available updates $updateSession = new-object -com "[Link]" write-progress -Activity "Updating" -Status "Checking available updates" $updates=$[Link]().Search($criteria).Updates $downloader = $[Link]() $[Link] = $Updates #If no updates available, do nothing if ($[Link] -eq "0") {
#Report to file if enabled if ($FileReport -eq $true) { "Invoke-WindowsUpdate was run on $env:ComputerName, but no new updates were found. Please try again later." | Out-File -FilePath $Path } } else { #If updates are available, download and install write-progress -Activity 'Updating' -Status "Downloading $($[Link]) updates" $Criteria="IsInstalled=0 and Type='Software'" $resultcode= @{0="Not Started"; 1="In Progress"; 2="Succeeded"; 3="Succeeded With Errors"; 4="Failed" ; 5="Aborted" } $Result= $[Link]()
if (($[Link] -eq 0) and (($[Link] eq 2) -or ($[Link] eq 3)) ) { $updatesToInstall = New-object -com "[Link]" $Updates | where {$_.isdownloaded} | foreach-Object {$[Link]($_) | out-null } $installer = $[Link]() $[Link] = $updatesToInstall write-progress -Activity 'Updating' -Status "Installing $($[Link]) updates" $installationResult = $[Link]() $Global:counter=-1 $Report = $[Link] | Select-Object -property Title,EulaAccepted,@{Name='Result';expression={$ResultCode[$[Link] pdateResult($Global:Counter++).resultCode ] }},@{Name='Reboot required';expression={$[Link]($Global:Counter++).RebootR equired }} |ConvertTo-Html
#Report to file if enabled if ($FileReport -eq $true) { $Report | Out-File -FilePath $path } #Reboot if this is autostart ne necessary if ($autoRestart -and $[Link]) { [Link] /t 0 /r } } } List Installed Software #replace with your computers name $strComputer = " ChucksPC " $colInstalled = get-wmiobject -class "Win32_Product" -namespace "root\CIMV2" ` -computername $strComputer foreach ($objItem in $ colInstalled) { write-host "Caption: " $[Link] write-host "Description: " $[Link] write-host "Identifying Number: " $[Link] write-host "Installation Date: " $[Link] write-host "Installation Date 2: " $objItem.InstallDate2 write-host "Installation Location: " $[Link]
write-host "Installation State: " $[Link] write-host "Name: " $[Link] write-host "Package Cache: " $[Link] write-host "SKU Number: " $[Link] write-host "Vendor: " $[Link] write-host "Version: " $[Link] write-host } List Startup Items #replace with your computers name $strComputer = " ChucksPC " $colStartUp = get-wmiobject -class "Win32_LogicalProgramGroupItem" -namespace "root\CIMV2" ` -computername $strComputer foreach ($objItem in $ colStartUp) { write-host "Caption: " $[Link] write-host "Description: " $[Link] write-host "InstallationDate: " $[Link] write-host "Name: " $[Link] write-host "Status: " $[Link] write-host } Get drives and sizes $Disk = get-WmiObject win32_logicaldisk foreach ($Drive in $Disk) { $[Link] + " - " + [INT] ($[Link] / 1048576) + " MB" } Get Time Details $strComputer = . $colItems = get-wmiobject -class Win32_LocalTime -namespace root\CIMV2 ` -computername $strComputer foreach ($objItem in $colItems) { write-host Day: $[Link] write-host Day Of Week: $[Link] write-host Hour: $[Link] write-host Milliseconds: $[Link] write-host Minute: $[Link] write-host Month: $[Link] write-host Quarter: $[Link] write-host Second: $[Link] write-host Week In Month: $[Link] write-host Year: $[Link]
write-host } Create an OU in Active Directory $StrOUName = Read-Host "Enter New OU Name" Clear #replace MyDomainName with your domain $objDomain = [ADSI]"LDAP://dc=MyDomainName,dc=Com" $objOU = $[Link]("OrganizationalUnit", "ou=" + $StrOUName) $[Link]() Write-Host $StrOUName + " has been created"
Create a User #replace with YOUR DOMAIN INFO $objOU = [ADSI]"LDAP://dc=MyDomainName,dc=Com" $objUser = $[Link]("user", "cn=MyerKen") $[Link]("sAMAccountName", "myerken") $[Link]() Create a Group $objOU = [ADSI]"LDAP://dc=MyDomainName,dc=Com" $objGroup = $[Link]("group", "cn=Atl-Users") $[Link]("sAMAccountName", "Atl-Users") $[Link]() Get Processor Info $computer = "LocalHost" $namespace = "root\CIMV2" Get-WmiObject -class Win32_Processor -computername $computer -namespace $namespace Get Bios Info $computer = "LocalHost" $namespace = "root\CIMV2" Get-WmiObject -class Win32_BIOS -computername $computer -namespace $namespace Get users on PC (Remote or local) #change computer name to match yours $strComputer = "ChucksPC" $computer = [ADSI]("WinNT://" + $strComputer + ",computer") $[Link] $Users = $[Link] |where{$_.[Link] -eq "User"} foreach ($member in $[Link]) {$[Link]} Create a Share $FolderPath = "C:\Temp"
$ShareName = "MyShare" $Type = 0 $objWMI = [wmiClass] 'Win32_share' $[Link]($FolderPath, $ShareName, $Type) Get all computers in a domain $strFilter = "computer" $objDomain = New-Object [Link] $objSearcher = New-Object [Link] $[Link] = $objDomain $[Link] = "Subtree" $[Link] = 1000 $[Link] = "(objectCategory=$strFilter)" $colResults = $[Link]() foreach ($i in $colResults) { $objComputer = $[Link]() Get-WMIObject Win32_BIOS -computername $[Link] } Do an SQL Server Database Backup [Link]]::LoadWithPartialName('[Link]') | outnull $s = New-Object ('[Link]') "LOCALHOST\" #Create a Backup object instance with [Link] $dbBackup = new-object ("[Link]") #Set the Database property to Northwind $[Link] = "Northwind" #Add the backup file to the Devices collection and specify File as the backup type $[Link]("D:\PSScripts\backups\TestBack_FULL.bak", "File") #Specify the Action property to generate a fullbackup $[Link]="Database" #Call the SqlBackup method to generate the backup $[Link]($s)
Does a File Exist? # Setup source and destination files $SourceFile = "c:\test\[Link]";
$NewFile = "c:\test\[Link]"; # Now - check to see if $Sourcefile exists, and if # it does copy it to $newfile if ([[Link]]::Exists($SourceFile)) { [[Link]]::Copy($SourceFile, $NewFile) "Source File ($SourceFile) copied to ($newFile)" } else { "Source file ($Sourcefile) does not exist." } Copy a file Copy-Item -Path c:\[Link] -Destination c:\somefolder\[Link] Excel and PowerShell objExcel = New-Object -comobject [Link] $[Link] = $True $objWorkbook = $[Link]() $objWorksheet = $[Link](1) $[Link](1,1) = "A value in cell A1." $[Link]("C:[Link]") $[Link]()