Skip to content

Instantly share code, notes, and snippets.

@NicholasLeader
Created October 3, 2022 17:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NicholasLeader/8af986cfa211892a5fdb5ee44d229762 to your computer and use it in GitHub Desktop.
Save NicholasLeader/8af986cfa211892a5fdb5ee44d229762 to your computer and use it in GitHub Desktop.
PowerShell to look up details between 2 user accounts that are similarly named / contains same substring - using split & calculated property
<#
Nicholas Leader
10.03.2022
script to:
pull AD accounts with a preceding 'a-', that are enabled
using '-ResultPageSize 1000' to avoid an AD DC server side error message 'invalid enumeration context'
using 'split' to return the normal user AD samaccount name and return the associated 'company' value on the normal user account
#>
#date stamp in string
$DateStamp = get-date -Format MM.dd.yyyy.mm
get-aduser -filter {(samaccountname -like 'a-*') -and (enabled -eq "TRUE") } -properties company -ResultPageSize 1000 |
select-object name,samaccountname, enabled,@{ name = 'NONaCompany'; expression = {(get-aduser -properties company ((get-aduser $_).samaccountname -split( "-"))[1]).company }} |
export-csv -NoTypeInformation "a-report.$DateStamp.csv"
#example splitting of 'a-' account to return normal account
# ((get-aduser a-exampleUser).samaccountname -split( "-"))[1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment