Forum Discussion

charlie4872's avatar
charlie4872
Brass Contributor
Oct 29, 2021
Solved

Need to change bulk users Samaccountname to uppercase

Hello I am trying to get this script to update a list of samaccountname from lowercase to uppercase. We have an application that has issues with lowercase. I have tried the below and it is giving me a "You cannot call a method on a null-valued expression" error when running. I am not sure what I am doing wrong but any help will be greatly appreciated. Here is the script I am using now.

 

$users = get-content '.\users.txt'
foreach($user in $users){
set-aduser $user -SamAccountName $user.samaccountname.toupper() 
}
  • Here's a working example:

    $users = Import-Csv .\users.csv
    $users | % { Set-ADUser $_.SamAccountName -SamAccountName $_.SamAccountName.ToLower() }
  • For that to work, you need to use a CSV file with a column SamAccountName to designate the user (and Import-CSV instead of Get-Content).
    • charlie4872's avatar
      charlie4872
      Brass Contributor
      Thanks for the reply Vasil. When creating the .csv file and adding the column. Running the script now gives me the below error.

      set-aduser : Object reference not set to an instance of an object
      At C:\Scripts\Domain MIgration\TEST.ps1:3 char:1
      + set-aduser $user -SamAccountName $user.samaccountname.toupper()
      • VasilMichev's avatar
        VasilMichev
        MVP
        Here's a working example:

        $users = Import-Csv .\users.csv
        $users | % { Set-ADUser $_.SamAccountName -SamAccountName $_.SamAccountName.ToLower() }

Resources