locked
Having an issue with an easy script....... RRS feed

  • Question

  •  Here is my script:

    Import-module ActiveDirectory

    $Users = Import-CSV C:\EmployeeList.csv

    Foreach($User in $Users)

    {Set-ADUser -identity $_.Sam -OfficePhone $_.DID}

    ######################################

    Here is how my CSV is set up:

    First Name Last Name     Ext        DID          Sam
    Steve          Nunez         7890 1234567890 Snunez

    The SNunez is a concatenate in Excel, I thought maybe it didn't like that so I just typed SNunez in, but it didn't work either.

    I am trying to change the office phone in bulk. I am getting the following error:

    Set-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null......Any help would be greatly appreciated. Thank you in advance.

    Matthew

     

    Wednesday, April 8, 2015 1:32 AM

Answers

  • Hi Matthew,

    Since you're using a foreach loop, you need to use $User instead of $_ inside of it.


    EDIT: Examples:

    #foreach loop
    $users = Import-Csv C:\EmployeeList.csv 
    
    foreach ($user in $users) {
    
        Set-ADUser -Identity $user.Sam -OfficePhone $user.DID -WhatIf
        
    }
    
    
    #foreach-object loop
    Import-Csv C:\EmployeeList.csv | ForEach-Object {
    
        Set-ADUser -Identity $_.Sam -OfficePhone $_.DID -WhatIf
    
    }


    Don't retire TechNet! - (Don't give up yet - 13,225+ strong and growing)

    • Edited by Mike Laughlin Wednesday, April 8, 2015 2:20 AM
    • Proposed as answer by Chen VMVP Wednesday, April 8, 2015 10:51 AM
    • Marked as answer by Boe ProxMVP Sunday, July 26, 2015 3:30 AM
    Wednesday, April 8, 2015 2:15 AM

All replies