I am writing a code to update an Active Directory using input from a CSV file. I am able to get the script to run all of the way to the end, but when I check the Active Directory, it is not updated. When I check the log file, it does not list any CSV file as being used. Everything is in the same folder and the log file writes fine.
I have tried altering the path that the script searches for the CSV file to C:/PATH/TO/FILE/filename I have also tried with quotes, without quotes, with -Path, and without -Path. I have looked at the other StackOverflow articles and they all seem to say that this should work... I think the key line here is the 3rd from the top. "$Users = Import-csv -Path users.csv" I could be wrong though.
Import-Module ActiveDirectory
#Import and Specify CSV File
$Users = Import-csv -Path users.csv
#Specify Log File
$LogFile = ".\UpdateUsersCsv.log"
# Write to the log file. Abort the script if the log file cannot be updated.
Add-Content -Path $LogFile -Value "================================================"
Add-Content -Path $LogFile -Value "UpdateUsersCsv.ps1"
Add-Content -Path $LogFile -Value "Update Users from CSV file: $Users"
Add-Content -Path $LogFile -Value "Started: $((Get-Date).ToString())"
Add-Content -Path $LogFile -Value "Log file: $LogFile"
Add-Content -Path $LogFile -Value "------------------------------------------------"
# Initialize counters.
$Updated = 0
$NotFound = 0
#Find Users
foreach($User in $Users){
$ADUser = Get-ADUser
#If found, update Users and increment $Updated
If($ADUser){
Set-ADUser $User.SamAccountName -Description $User.NewDescription
$Updated = $Updated + 1
#If not found, increment $NotFound
}ElseIf(!$ADUser){
Add-Content -Path $LogFile -Value "User $ASUser not found."
$NotFound = NotFound + 1
}
}
Add-Content -Path $LogFile -Value "Number of users updated: $Updated"
Add-Content -Path $LogFile -Value "Number of users not found: $NotFound"
Add-Content -Path $LogFile -Value "================================================"
Write-Host "Script Completed, check log file $LogFile for more information."
The Active directory should be updated with whatever information is in the CSV file. For example, if I used : myName, myDescription goes here It should update the description of the user called "myName" to say "myDescription goes here" Then the log file should say something along the lines of: From csv file: nameOfCSV Users updated = 1 Users not found = 0
If the user is not found, it should report that accordingly, but that is not my current problem.
When I run the script and open the log file, there is no csv file shown, and the users updated and users not found fields are left at 0. This leads me to believe that the problem is that the csv file is not importing.

$Usersline, is anything in that $Var?ElseIf(!ADUser)should beElseIf(!$ADUser)$Users.$Usersis an array of objects representing the users you want added/updated.Add-Content -Path $LogFile -Value "Update $($Users.count) Users from CSV file: Users.csv"That way if it says 1 or more users you know it loaded the CSV. Otherwise set a variable like$CSVPath = Resolve-Path 'users.csv'and then use that when importing it and when logging it.$ADUser = Get-ADUserbe$ADUser = Get-ADUser $user.<HeaderUsedInTheCSVforTheUserColumn>