Update user AD attributes from CSV file using powershell - powershell

I'm trying to update AD user attributes:title, physicalDeliveryOfficeName and department from CSV file using powershell. I am new to powershell so I need some help. (please, and thanks in advance)
So, the idea is that filter for a match is displayName attribute and the script I use is:
Import-Module ActiveDirectory
$csv = Import-Csv C:\Temp\AD_import_test.csv
foreach ($line in $csv) {
$displayName = $line.displayName
Get-ADUser -Filter {displayName -eq $displayName} |
Set-ADUser -Title $($csv.title) -department $($csv.Department) -physicalDeliveryOfficeName $($csv.physicalDeliveryOfficeName) }
But the script is returning error:
Set-ADUser : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Title'. Specified method is not supported.
At line:6 char:19
+ Set-ADUser -Title $($csv.title) -department $($csv.Department) -physi ...
+ ~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-ADUser], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,Microsoft.ActiveDirectory.Management.Commands.SetADUser
Set-ADUser : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Title'. Specified method is not supported.
At line:6 char:19
+ Set-ADUser -Title $($csv.title) -department $($csv.Department) -physi ...
+ ~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-ADUser], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,Microsoft.ActiveDirectory.Management.Commands.SetADUser

You're using the wrong variable to assign those values. In your script $csv contains the entire .csv file, but within your foreach line you're loading each individual line into $line, and it's that which you need to set your value.
So in essence, $($csv.title) equals ALL of the title fields, not just the one you're interested in. That's why you're getting the error saying it's a System.Object[] rather than System.String... because it's not one single value.
I notice you have it correct on the "$displayName = $line.displayName" line, so it's just the Set-ADUser line that needs updating, as :
Set-ADUser -Title $($line.title) -department $($line.Department) -physicalDeliveryOfficeName $($line.physicalDeliveryOfficeName) }
Edit:
Also, with something like this a useful trick is to add some Write-Output lines to show what the variable contents are, so you can check if they match what you're expecting, for instance :
Write-Output $($line.title)
which then in the case of $line.title should show you a single value, whereas if you did $($csv.title) it would return multiple values and show you where the issue is.

You code seems good for me except the set-aduser you need to replace $csv with $line
Import-Module ActiveDirectory
$csv = Import-Csv C:\Temp\AD_import_test.csv
foreach ($line in $csv) {
$displayName = $line.displayName
Get-ADUser -Filter {displayName -eq $displayName} |
Set-ADUser -Title $($line.title) -department $($line.Department) -physicalDeliveryOfficeName $($line.physicalDeliveryOfficeName) }

Related

Active directory delete description and office fields through powershell

I have a list of users in active directory to which I have to remove the description and office fields. how can i do through poweshell? I try this but not work.
My code path is ok
Import-Module ActiveDirectory
$Users = Import-csv C:\Users\xxxx\Desktop\test.csv
foreach($User in $Users){
Set-ADUser $User.SamAccountName -Description $User.NewDescription
}
my csv
SamAccountName;NewDescription;EmailAddress
xxxxxxxxxx;xxxxxxxxxxx;xxxxxxxxxxxxx#libero.it
Powershell answer
Set-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null. Provide a valid value for the
argument, and then try running the command again.
At line:4 char:12
+ Set-ADUser $User.SamAccountName -Description $User.NewDescription
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Set-ADUser], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.SetADUs
er
Help will be appreciated
You need to specify your delimiter on your import-csv statement. The default is comma separated, so you can define it like so
$Users = Import-csv C:\Users\xxxx\Desktop\test.csv -Delimiter ';'
Without this, then it's importing as one single column instead of three.
See Import-CSV for more.
It might work?
$users = Import-Csv -Path C:\users1.csv
foreach ($user in $users) {
#Search in specified OU and Update existing attributes
Get-ADUser -Filter “SamAccountName -eq ‘$($user.samaccountname)'” -Properties * -SearchBase “cn=Users,DC=**********,DC=*****” |
Set-ADUser -Clear description, Office
}

Cannot Search AD from CSV using Powershell

Trying to search AD account properties pulling from a CSV. The Import-CSV line works by itself. I cannot for the life of me figure out why it is asking for a filter. I took this from another script I found where they said it worked. Others were using a For-Each statement.
PS C:\Users\XXXXX> Import-CSV .\listofnames.csv | Get-ADUser $_.DisplayName -properties displayname
Get-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null or an element of the argument
collection contains a null value.
At line:1 char:43
+ Import-CSV .\listofnames.csv | Get-ADUser $_.DisplayName -properties ...
+ ~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-ADUser], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.GetADUser
Its prompting you for the 'Identity' because that is how it identifies what user you are searching for.
Try this:
$users = get-content C:\Temp\test.csv
foreach ($user in $users){Get-ADUser -Identity $user -Properties displayname}
The CSV file just has the user IDs for the users who you would like to find info for.
Or if you can try the following:
Import-Csv C:\Temp\users.csv | ForEach-Object { Get-ADUser -identity $_.Name -Properties displayname }

How can I change the display name in Active Directory with powershell?

How can I change the Name attribute in Active Directory in powershell?
I would like to change the 'Name' row, but when I enter the following I get an error:
Set-ADuser -Identity test1 -Name Test 11
The error message:
Set-ADUser : A parameter cannot be found that matches parameter name 'Name'.
At line:1 char:28
+ Set-ADUser -Identity test1 -Name Test 11
+ ~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-ADUser], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.SetADUser
You should use the cmdlet Rename-ADObject to change the name attribute of the AD object.
If you want to change multiple properties for an account in one go (say changing a users name), add the PassThru param to Set-AdUser and then pipe to Rename-ADObject:
Set-ADUser -Identity "test1" -DisplayName "DisplayName" -GivenName "GivenName" -Surname "Surname" -PassThru | Rename-ADObject -NewName "TestAccount1" -PassThru

Variable within quotes fails

I am using a variable in PowerShell to get Active Directory info.
The variable does not get the value I need in double quotes. What am I doing wrong?
The variable display correctly but in the Move-AdObject cmdlet the value
is $CN and not tok12.
Here is the code:
$CN = "tok12"
$company = "acme"
$CN
$company
Get-ADGroup $CN | Set-ADGroup -Description $company
Start-Sleep -s 1
Move-ADObject -Identity "CN=$CN,OU=UnassignedPortalTokens,OU=Portal,OU=testdomain,DC=testdomain,DC=com" -TargetPath "OU=Customers,OU=testdomain,DC=testdomain,DC=com"
Error message:
Get-ADGroup : Cannot find an object with identity: 'Kentest' under:
'DC=testdomain,DC=com'.
At C:\PSH\Newport.ps1:13 char:2
+ Get-ADGroup $company | Set-ADGroup -Description "$token"
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Kentest:ADGroup) [Get-ADGroup], ADIdentityNotFoundException
+ FullyQualifiedErrorId : Cannot find an object with identity: 'Kentest' under: 'DC=testdomain,DC=com'.,Microsoft.ActiveDirectory.Management.Commands.GetADGroup
Question remains quite unclear, but OP seems to have fixed the issue by himself:
(...) one of the commands was a wrong group. The other issue was the
variable was null and I had to remove and set it again from command
line – Gary Seven

How to update user attributes?

I'm trying to write a script that updates every user's msIIS-FTPRoot attribute where the user's description = ‘FTPuser’.
import-module activedirectory
$users = Get-ADUser -SearchBase "dc=NAME,dc=com" -Filter {Description -eq "FTPUser"}
Set-ADUser $users -Replace #{msIIS-FTPRoot='NewTEXT'}
I get the following error:
Missing '=' operator after key in hash literal.
At line:1 char:60
+ Set-ADUser $users -Replace #{msIIS-FTPRoot='\\SOMETEXT\' <<<< }
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingEqualsInHashLiteral
Using quotation marks on the attribute name solves your problem.
Set-ADUser $users -Replace #{'msIIS-FTPRoot'='NewTEXT'}