CLI
Refresh environment variables without reboot windows
- open cmd commend prompt window
- input: set PATH=C -> this will refresh the environment variables
- close and restart cmd window
- input: echo %PATH% to test
- can use setx command to set variable permanentlyFind current loggged-on user
get-adcomputer $computername: Get computer details
quser /server:$servername
get-aduser $SamAccountName (from above command)
Oneliner
Get-ADuser $(quser /server:$servername | ForEach-Object { ($_ -replace '^\s+|\s+$','') -split '\s{2,}' | Select-Object -First 1 } | select-object -last 1)
# If there's multiple users
quser /server:$servername | ForEach-Object { ($_ -replace '^\s+|\s+$','') -split '\s{2,}' | Select-Object -First 1 } | Where-Object {$_ -notmatch 'USERNAME'} | foreach-object {get-aduser $_}
# Explanation of the replace part:
ForEach-Object {
($_ -replace '^\s+|\s+$','') # Step 1: Trim leading/trailing whitespace
-split '\s{2,}' # Step 2: Split the cleaned line by 2+ spaces
| Select-Object -First 1 # Step 3: Select the first token (usually the username)
}Find User accoung using Full Name
Get-ADUser -Filter "Name -like 'First Last'" -Properties DisplayName | Select-Object Name, SamAccountName, DisplayName
List member of AD Group
Get-ADGroupMember -Identity "Group Name" | Where-Object {$_.samAccountName -like "keyword"} | Where-Object samAccountName,DistinguishedName
Get-ADGroupMember -Identity "Group Name" | Where-Object {$_.samAccountName -like "keyword"} | Where-Object samAccountName,DistinguishedName | Export-csv -Path "file.csv" -NoTypeInformation
Last updated