SOLVED

Need to change bulk users Samaccountname to uppercase

Brass Contributor

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() 
}
4 Replies
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).
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()
best response confirmed by charlie4872 (Brass Contributor)
Solution
Here's a working example:

$users = Import-Csv .\users.csv
$users | % { Set-ADUser $_.SamAccountName -SamAccountName $_.SamAccountName.ToLower() }
That worked. Thanks for your help Vasil!
1 best response

Accepted Solutions
best response confirmed by charlie4872 (Brass Contributor)
Solution
Here's a working example:

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

View solution in original post