Hi All<\/p>\n
i have a single csv file which contains samaccount names and upns. i mean some are samaccountnames and some ups in the same csv file. i am trying the below syntax but i am not getting the output. experts guide me.<\/p>\n
$list = Import-Csv C:\\temp\\input.csv\n ForEach($user in $list){ \n $dn = $user.user\n Get-ADUser -Filter $dn -Properties DisplayName,SamAccountName,EmailAddress,Userprincipalname,Office,co|Select DisplayName,SamAccountName,EmailAddress,Userprincipalname,Office,co |Export-Csv C:\\temp\\output.csv -NoType -Append }\n<\/code><\/pre>","upvoteCount":10,"answerCount":6,"datePublished":"2021-10-01T07:36:49.000Z","author":{"@type":"Person","name":"risingflight","url":"https://community.spiceworks.com/u/risingflight"},"acceptedAnswer":{"@type":"Answer","text":"
Advertisement
Just as a style note, your Get-ADUser command line is going to become cumbersome.<\/p>\n
To break out the definitions and clean up the code, you can put the parameters for Get-ADUser into variables thus:<\/p>\n
$NotWanted = @(\n\t\"cctvdvr\",\n\t\"DiscoverySearch\",\n\t\"FAX_MAILER\",\n\t\"FederatedEmail\",\n\t\"IUSR\",\n\t\"noreply\",\n\t\"Scanner\",\n\t\"Spam Hero\",\n\t\"Support\",\n\t\"SystemMailbox\") # Eliminate duplicates by using regex wildcards\n\n[RegEx]$Exclusions = ($NotWanted -Join \"|\") # Build the RegEx at runtime\n\n$WantedFields = @(\n\t'Name',\n\t'Initials',\n\t'MobilePhone',\n\t'OfficePhone',\n\t'UserPrincipalName')\n$ADParms = @{\n\tFilter = {\n\t\t(Enabled -ne $False) -and \n\t\t(Initials -like '*')\n\t\t}\n\tProperties = $WantedFields\n\t} # Parameters for the Get-ADUser to follow\n\n##\t\t\tRoll through the list, dumping them all\n(Get-ADUser @ADParms | Where-Object {$_.Name -NotMatch $Exclusions} | Sort Initials) |\n ForEach {\n#[... do your thing ...]\n}\n<\/code><\/pre>\nI know this isn’t an answer to your question, but I think you’ll soon find it useful.<\/p>","upvoteCount":0,"datePublished":"2021-10-05T11:25:14.000Z","url":"https://community.spiceworks.com/t/get-aduser-filter/812798/6","author":{"@type":"Person","name":"jimlong3","url":"https://community.spiceworks.com/u/jimlong3"}},"suggestedAnswer":[{"@type":"Answer","text":"
Hi All<\/p>\n
i have a single csv file which contains samaccount names and upns. i mean some are samaccountnames and some ups in the same csv file. i am trying the below syntax but i am not getting the output. experts guide me.<\/p>\n
$list = Import-Csv C:\\temp\\input.csv\n ForEach($user in $list){ \n $dn = $user.user\n Get-ADUser -Filter $dn -Properties DisplayName,SamAccountName,EmailAddress,Userprincipalname,Office,co|Select DisplayName,SamAccountName,EmailAddress,Userprincipalname,Office,co |Export-Csv C:\\temp\\output.csv -NoType -Append }\n<\/code><\/pre>","upvoteCount":10,"datePublished":"2021-10-01T07:36:49.000Z","url":"https://community.spiceworks.com/t/get-aduser-filter/812798/1","author":{"@type":"Person","name":"risingflight","url":"https://community.spiceworks.com/u/risingflight"}},{"@type":"Answer","text":"The CSV needs to have a header line. In this case if the header is named “user”, this code should work. However, if the DN’s have spaces in them, you need to wrap each one in double quotes in your CSV first. Example of CSV data below.<\/p>\n