question

QuincyNewburn-1046 avatar image
0 Votes"
QuincyNewburn-1046 asked AndreasBaumgarten commented

Having trouble with this powershell script

Trying to update a specific user's description and company using PowerShell using a csv file using this script

$csv = Import-Csv C:\Users\qnewburn\Desktop\Qnewb.csv"
foreach ($line in $csv) {
$UserDisplayname = $line.DisplayName
Get-ADUser -Filter {UserDisplayname -eq $UserDisplayname} |
Set-ADUser -Description $($csv.Description)
Company $($csv.company) }



[1]: /answers/storage/attachments/119898-screenshot-2021-08-02-101825.png

windows-server-powershell
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @QuincyNewburn-1046 ,

are there any additional questions? Does the answer help to solve the issue?
If you found the answer helpful, it would be great if you please mark it "Accept as answer". This will help others to find answers in Q&A


Regards
Andreas Baumgarten

0 Votes 0 ·
AndreasBaumgarten avatar image
0 Votes"
AndreasBaumgarten answered

Hi @QuincyNewburn-1046 ,

please remove the [space] in Display Name -> DisplayName in the csv file.

Than please try the script I posted above.


(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AndreasBaumgarten avatar image
1 Vote"
AndreasBaumgarten answered RichMatheisen-8856 commented

Hi @QuincyNewburn-1046 ,

please give this a try (not tested by myself!)

 $csv = Import-Csv C:\Users\qnewburn\Desktop\Qnewb.csv"
 foreach ($line in $csv) {
 $UserDisplayname = $line.DisplayName
 Get-ADUser -Filter {UserDisplayname -eq $UserDisplayname} |
 Set-ADUser -Description $line.Description -Company $line.company }


(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Im so close with this one but I am getting a error saying "Import-Csv : A positional parameter cannot be found that accepts argument 'ForEach'."



$Users = Import-CSV "C:\Users\qnewburn\Desktop\Qnewb.csv" -Delimiter ',' ForEach ($Users In $User) { Get-ADUser -Filter * -property displayname | where { $_.displayname -eq $User.EmployeeFullName} | Set-ADUser -Description $User.Description -Company $Users.Company }

0 Votes 0 ·

The "ForEach" is being taken as a parameter of the Import-CSV.

Put the "ForEach" on another line. Or put a ";" right before the "ForEach".

1 Vote 1 ·

Hi @QuincyNewburn-1046 ,

please use Sample Code Ctrl+K to post code.
The Q&A text editor is screwing up code if posted as normal text.

Also please post an example what's in the csv file (one line is enough)

Have you tried the code I posted above?


(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten

0 Votes 0 ·
QuincyNewburn-1046 avatar image
0 Votes"
QuincyNewburn-1046 answered

119916-image.png



image.png (4.2 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.