Windows Administrator Must Have Powershell Commands

This is a quick blog post about some useful windows powershell commands an administrator will find useful on a day to day basis. Share the love

CommandWhat it Does?
$psversiontableWhich version of powershell and I running
Set-ExecutionPolicy -Scope Process -ExecutionPolicy BypassAllow execution of commands
Get-Printer | select Name, DriverName, PortName | Export-Csv Printers.csv -NoTypeInformationList printers on a server
Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | Format-Table –AutoSize >C:\temp\installed.txtList installed programs on server
Get-ADDomainController -Filter * | Format-table name,domain, forest,site, ipv4address, operatingsystemFind Domain Controllers
Get-Childitem -Path C:\windows -Recurse -Filter *.dllFind any *dll* on filesystem – recursive
 Get-ItemProperty .\install.xml | select *Get Detailed File information
Search-ADAccount -LockedOut | Format-Table name,lastlogondate, lockedout, objectclass, passwordexpired, passwordneverexpiresFind locked out users
Search-ADAccount -AccountDisabled -UsersOnly | FT Name,ObjectClass -AFind disabled users
Search-ADAccount -AccountInactive -TimeSpan 90.00:00:00 -UsersOnly  | FT Name,ObjectClass -AFind Users not logged in for 90 days
Search-ADAccount -AccountInactive -TimeSpan 90.00:00:00 -UsersOnly |Sort-Object | FT Name,ObjectClass -AFind Users not logged in for 90 days & Sort
Search-ADAccount -AccountInactive -TimeSpan 90.00:00:00 -UsersOnly |Sort-Object -property lastlogondate | FT Name,ObjectClass,lastlogondate, lockedout,  -AFind Users not logged in for 90 days & Sort by last logon 
Get-ADGroupMember -identity “Domain Admins” -recursive | select name | Export-csv -path C:\temp\Groupmembers.csv -NoTypeInformationFind “domain admins”
Get-ADComputer -Filter ‘Name -like “gbsyn*”‘ -Properties canonicalName,CN,created,IPv4Address,objectclass,OperatingSystem,OperatingSystemServicePackFind Computer with GBSYN* in the name
Get-ADPrincipalGroupMembership -identity ssi_rbailey | Sort-object | FT -property name, samaccountname -AutoSizeFind which AD groups a user is member of
Get-ADGroupMember -Identity “Domain Admins” -Recursive | %{Get-ADUser -Identity $_.distinguishedName -Properties Enabled | ?{$_.Enabled -eq $false}} | Select DistinguishedName,Enabled | Export-Csv c:\temp\result.csv -NoTypeInformationFind Disabled Domain admins
Get-ADGroupMember -Identity “Domain Admins” -Recursive | %{Get-ADUser -Identity $_.distinguishedName -Properties Enabled | ?{$_.Enabled -eq $false}} | Select Name,SamAccountName,Enabled, objectclass | sort-object -Property nameFind Disabled Domain admins & Sort
Get-ADGroupMember -Identity “Domain Admins” -Recursive | %{Get-ADUser -Identity $_.distinguishedName -Properties Enabled | ?{$_.Enabled -eq $false}} | Select Name,SamAccountName,Enabled, objectclass | sort-object -Property name | Export-CSV C:\temp\DisabledDomainAdmins.csvFind Disabled Domain admins & Sort & Export to CSV
Get-ADGroupMember -Identity “GROUPNAME” -Recursive | %{Get-ADUser -Identity $_.distinguishedName -Properties Enabled | ?{$_.Enabled -eq $true}} | Select Name,EnabledFind members in a group
Get-Eventlog application -Newest 2000 | Where-Object {$_.entryType -Match “Error”}Find Latest errors in Eventlog
Get-Eventlog system -Newest 2000 | Where-Object {$_.entryType -Match “Error”}Find Latest errors in Eventlog
cd hkcu:Navigate Registry like filesystem
ps | sort –p ws | select –last 5Find the five processes using the most memory
Restart-Service DHCPRestar DHCP service
Get-ChildItem – ForceList all items within a folder
Get-ChildItem –Force c:\directory –RecurseList all items within a folder & all subfolders
Get-WmiObject -Class Win32_Service -Filter “state = ‘running’ and startmode = ‘auto’” | Select-Object name, startmode, description | Format-table -AutoSizeGet list of Services that start automatically
[System.Reflection.Assembly]::LoadWithPartialName(‘Microsoft.SqlServer.SMO’)Load SQL modules into memory 
[System.Reflection.Assembly]::LoadWithPartialName(‘Microsoft.SqlServer.SMO’) | out-null
$s = New-Object (‘Microsoft.SqlServer.Management.Smo.Server’) “servername”
$dbs=$s.Databases
$dbs | SELECT Name, Collation, CompatibilityLevel, AutoShrink, RecoveryModel
Discover SQL instances
Get-VIEvent -maxsamples 10000 | where {$_.Gettype().Name -eq”VmRemovedEvent”} | Sort CreatedTime -Descending | Select CreatedTime, UserName, FullformattedMessage -First 19Find Vm’s Remove last 10 days
$wu = new-object -com “Microsoft.Update.Searcher”
 $totalupdates = $wu.GetTotalHistoryCount()
 $all = $wu.QueryHistory(0,$totalupdates)
 # Define a new array to gather output
 $OutputCollection=  @()        
Foreach ($update in $all)
    {
    $string = $update.title
    $Regex = “KB\d*”
    $KB = $string | Select-String -Pattern $regex | Select-Object {$_.Matches }
     $output = New-Object -TypeName PSobject
     $output | add-member NoteProperty “HotFixID” -value $KB.‘ $_.Matches ‘.Value
     $output | add-member NoteProperty “Title” -value $string
     $OutputCollection += $output
    }
 # Output the collection sorted and formatted:
$OutputCollection | Sort-Object HotFixID | Format-Table -AutoSize
Write-Host “$($OutputCollection.Count) Updates Found
Command to get all Installed KB (Windows Update) from Server
frminst.exe /remove=agentForce uninstall McAffee when managed by another server (managed mode)

You May Also Like

About the Author: VirtuallyThatGuy

Leave a Reply

Your email address will not be published. Required fields are marked *