Powershell Snippets

List local user accounts

Get-WmiObject -Class Win32_UserAccount -Filter "LocalAccount='True'"


List local user accounts and parse array

$local_users = Get-WmiObject -Class Win32_UserAccount -Filter "LocalAccount='True'" | findstr "Caption";
for ($i = 0; $i -lt $local_users.Count; $i++) {
    Write-Host $local_users[$i].split("\\")[1];
}


List all installed Windows updates

Get-Hotfix-computername <hostname>Select HotfixID, InstalledOn |Sort-Object InstalledOn
Output will look something like:

tail equivalent in Powershell

Get-Content filename.txt -Tail 30

List all AD groups for user

Save the following code to a script called getgroups.ps1:

Param(   
    [Parameter(Mandatory=$false)][string]$username 
) 
(New-Object System.DirectoryServices.DirectorySearcher(
    "(&(objectCategory=User)(samAccountName=$($username)))"
 )).FindOne().GetDirectoryEntry().memberOf

Run the script from powershell and enter username as parameter, like this:

.\getgroups.ps1 johndoe

Was this post helpful?
Yes
No
Powered by Devhats

One thought on “Powershell Snippets”

Leave a Reply

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