SHARE
TWEET

[PS]AD QUERIES

pwrusr-com Jun 1st, 2020 (edited) 574 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Get-ADUser -Filter {(mail -ne "null") -and (Enabled -eq "true")} -Properties Surname,GivenName,mail | Select-Object Name,Surname,GivenName,mail | Format-Table
  2. # Active AD User Accounts with an email address.
  3.  
  4.  
  5. Get-ADUser -Filter * -Properties EmailAddress | where -Property EmailAddress -eq $null
  6. # AD User accounts without an Email Address.
  7.  
  8.  
  9. Get-ADUser -Properties * -Filter {Enabled -eq 'True'} -SearchBase "OU=ExampleOU, DC=Domain, DC=com" | Select-Object @{Label = "Email";Expression = {$_.EmailAddress}}, @{Label = "First Name";Expression = {$_.GivenName}}, @{Label = "Last Name";Expression = {$_.Surname}}, @{Label = "Group";Expression = {($_.canonicalname -Split "/")[-2]}} | Export-Csv -Path users.csv -NoTypeInformation
  10. # CSV of all enabled users that are contained in the OU with Distinguished Name “OU=ExampleOU, DC=Domain, DC=com”
  11.  
  12.  
  13.  
  14. Get-ADUser -Filter * -Properties * | Select-Object -Property Name,SamAccountName,Description,EmailAddress,LastLogonDate,Manager,Title,Department,whenCreated,Enabled,Organization | Sort-Object -Property Name | ConvertTo-CSV
  15.    
  16. Get-ADUser -Filter * -Properties * | Select-Object -Property Name,SamAccountName,Description,EmailAddress,LastLogonDate,Manager,Title,Department,whenCreated,Enabled,Organization, @{$_.MemberOf |Get-Group|ForEach-Object {$_.Name}} | Sort-Object -Property Name | ConvertTo-CSV
  17.    
  18. Get-ADUser -Filter * -Properties * | Sort-Object -Property Name | ForEach-Object {
  19.     $_ | Format-List -Property Name,SamAccountName,Description,EmailAddress,LastLogonDate,Manager,Title,Department,whenCreated,Enabled
  20.     $_.MemberOf | Get-ADGroup | ForEach-Object {$_.Name} | Sort-Object
  21. }  | ConvertTo-CSV
  22.    
  23. $alist = "Name`tAccountName`tDescription`tEmailAddress`tLastLogonDate`tManager`tTitle`tDepartment`tCompany`twhenCreated`tAcctEnabled`tGroups`n"
  24. $userlist = Get-ADUser -Filter * -Properties * | Select-Object -Property Name,SamAccountName,Description,EmailAddress,LastLogonDate,Manager,Title,Department,Company,whenCreated,Enabled,MemberOf | Sort-Object -Property Name
  25. $userlist | ForEach-Object {
  26.     $grps = $_.MemberOf | Get-ADGroup | ForEach-Object {$_.Name} | Sort-Object
  27.     $arec = $_.Name,$_.SamAccountName,$_.Description,$_.EmailAddress,$_LastLogonDate,$_.Manager,$_.Title,$_.Department,$_.Company,$_.whenCreated,$_.Enabled
  28.     $aline = ($arec -join "`t") + "`t" + ($grps -join "`t") + "`n"
  29.     $alist += $aline
  30. }
  31. $alist | Out-File D:TempADUsers.csv
  32.    
  33. csvde -f test.csv
  34.    
  35. csvde -f test.csv
  36.    
  37. csvde -f test.csv -r objectClass=user
  38.    
  39. csvde -f test.csv -r objectClass=user -l DN, sAMAccountName, department, memberOf
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top