Post

Useful Powershell commands

PS:

#Enable / Command that allows us to get some attributes of active users:
Get-ADUser -f * -pr * -searchbase “dc=guler,dc=com” | where {$_.enabled -eq “Enabled”} | select name, sAMAccountName, employeeID, department, distinguishedname | export-csv c:\liste.csv -delimiter “;” -NoTypeInformation -Encoding UTF8

#To retrieve deleted users:
Get-ADObject -IncludeDeletedObjects -Filter {objectClass -eq “user” -and IsDeleted -eq $True} -Properties displayname, whencreated,whenchanged | select -Property displayname, whencreated,whenchanged | Export-csv -path C:\delete_user.csv -NoTypeInformation -Encoding Unicode

#To attract members from a group:
Get-ADGroupMember -identity “grup adı” | select samaccountname,name | Export-csv -path C:\grupuser.csv -NoTypeInformation

#Get a List of Users with Password Never Expires
Get-ADuser -filter * -properties Name, PasswordNeverExpires | where { $_.passwordNeverExpires -eq "true" } |  Select-Object DistinguishedName,Name,Enabled | Export-csv c:\pw_never_expires.csv -NoTypeInformation


#To retrieve the last login dates of users in a specific OU:
Get-ADUser -f * -pr * -searchbase “ou=test,dc=guler,dc=com” | where {$_.enabled -eq “Enabled”} | select name, lastlogindate | export-csv C:\user_lastlogon.csv -Encoding Unicode

#To pull computer objects in a particular OU:
Get-ADComputer -Filter * -SearchBase “OU=test, DC=guler, DC=com” -Properties OperatingSystem | Select Name, OperatingSystem | Format-Table -AutoSize | Out-File C:\comp.txt

#To get the computer number only:
Get-ADComputer -SearchBase “OU=TestOU,DC=guler,DC=com”` -Filter ‘OperatingSystem -like “*”‘ -Properties * | Select -Property Name,operatingSystem,@{Name=”LastLogon”; Expression={[DateTime]::FromFileTime($_.lastLogon).ToString()}} | # Export AD Computers to CSV file Export-CSV “C:\ADComputers.csv” -NoTypeInformation -Encoding UTF8

#sending a message to the screen of a device on the network:
$name = read-host "Enter computer name "
$msg = read-host "Enter your message "
Invoke-WmiMethod -Path Win32_Process -Name Create -ArgumentList "msg * $msg" -ComputerName $name
This post is licensed under CC BY 4.0 by the author.