1

I have a simple script to change part of the description for a user in AD.

Get-ADUser testuser -Properties description | ForEach-Object { 
    Set-ADUser $_.SamAccountName -Description "ChgDescript,$($_.Description.Split(',')[1])" 
}

However, I now have 700 users, where I need to change part of the description, using the command above. I'm unable to figure out the script correctly to import the .csv file and run the script against it.

$csvFile = "path_is_here"

Import-Module ActiveDirectory

Import-Csv $csvFile | Get-ADUser -Properties description | ForEach-Object { 
    Set-ADUser $_.SamAccountName -Description "ChgDescript,$($_.Description.Split(',')[1])" 
}

When I run the script above, I receive the following error:

Cannot validate argument on parameter 'Identity'.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
priara
  • 25
  • 1
  • 1
  • 5

3 Answers3

3

The error is coming from the Get-ADuser cmdlet and informing you that its -Identity parameter is null. I don't know the structure of your input file; but lets assume that its just a list of account names (so I added a header value of AccountName for readability), pass that property of your custom object created by your Import-Csv cmdlet to the Get-ADUser's -Identity parameter and see if it helps.

Unit Test Example:

Import-Csv -Path "C:\Temp\Users.txt" -Header "AccountName" | ForEach-Object {
    Get-ADUser -Identity $_.AccountName
}

Checkout method 3 in the help examples for Set-ADUser

http://go.microsoft.com/fwlink/?LinkID=144991

Modify the Manager property for the "saraDavis" user by using the Windows PowerShell command line to modify a local instance of the "saraDavis" user. Then set the Instance parameter to the local instance.

See if this works:

$Users = Import-Csv -Path "C:\test_users.txt" -Header "AccountName"
     foreach($User in $Users){ 
     $ADUser = Get-ADUser -Identity $User.AccountName -Properties Description 
     $ADUser.Description = "PUT YOUR MODIFIED DESCRIPTION HERE" 
     Set-ADUser -Instance $ADUser 
}
Jeff
  • 536
  • 6
  • 8
  • I'm getting a different error now: You cannot call a method on a null-valued expression. At line:7 char:21 + $_.description.split <<<< (',')[1] + CategoryInfo : InvalidOperation: (split:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull The script is: Import-Csv -Path "C:\test_users.txt" -Header "AccountName" | ForEach-Object { $user = Get-ADUser -Identity $_.AccountName -Properties Description foreach ($user in $user) { set-aduser $_.accountname -description "NewName,$($_.description.split(',')[1])" } } – priara Feb 02 '16 at 18:21
  • It's changing the description from "OldName, 1234567" to "NewName," I'm not sure why it's doing this. The csv file only contains "AccountName" and underneath it, usernames. – priara Feb 02 '16 at 18:26
  • OK. Look at the help example for Set-ADUser Method 3: Modify the Manager property for the "saraDavis" user by using the Windows PowerShell command line to modify a local instance of the "saraDavis" user. Then set the Instance parameter to the local instance. [link](http://go.microsoft.com/fwlink/?LinkID=144991) Try: $Users = Import-Csv -Path "C:\test_users.txt" -Header "AccountName" foreach($User in $Users){ $ADUser = Get-ADUser -Identity $User.AccountName -Properties Description $ADUser.Description = "**PUT YOUR MODIFIED DESCRIPTION HERE**" Set-aduser -Instance $ADUser } – Jeff Feb 04 '16 at 22:28
0

May you have to store the description in a variable before, like:

Get-Content users.txt | Get-ADUser  -Prop Description | Foreach {
$desc = $_.description + " Disabled 21122012 123456"
     Set-ADUser  $_.sAMAccountName -Description $desc
 }

(just copied from technet)

  • While storing a variable first can be helpful and improve code readability, it would be easier to understand the intent of your answer if it more directly addressed how to use it for the `-Identity` parameter mentioned in the question. The question's formatting has been edited after your answer was written, so it should be a bit easier to see what it's really asking for. – Booga Roo Feb 02 '16 at 05:48
0

Tell PowerShell which delimiter to use, when it is not comma: import-csv -Path "c:\test.csv" -Delimiter ";"

And it will work fine.