Answered by:
How to make set-aduser expect pipeline input

Question
-
I am running the command to update the manager field of a user:
Get-ADUser -identity sam | Select-Object -ExpandProperty samaccountname | Set-ADUser -identity $_ -Manager 'James'
However I am getting the error:
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.How can I get Set-ADUser to except pipeline input?
Thursday, December 20, 2018 3:19 PM
Answers
-
Simplest way, -identity aduser object gets passed over the pipe by value:
Get-ADUser sam | Set-ADUser -Manager James
You could do it this way if you want. $_ must be inside a script block.
Get-ADUser -identity sam | Select-Object -ExpandProperty samaccountname |
foreach-object { Set-ADUser -identity $_ -Manager 'James' }
or even this, since -identity accepts pipeline input, you can use a script block with it:
Get-ADUser -identity sam | Select-Object -ExpandProperty samaccountname |
Set-ADUser -identity { $_ } -Manager 'James'Friday, December 21, 2018 2:32 PM
All replies
-
Get-ADUser -identity sam | Set-ADUser -Manager (Get-ADUser -identity <<managerSamAccount>>)
No need to use ExpandProperty to get samAccountName. Manager property expects an ADUser object, so you need to use Get-ADUser to get the manager's object
If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''
- Edited by clayman2 Friday, December 21, 2018 1:15 PM fixed script
- Proposed as answer by LeeSeenLiMicrosoft contingent staff Monday, December 24, 2018 7:36 AM
Thursday, December 20, 2018 4:10 PM -
Actually, you don't need to select anything from the first Get-ADUser. Just piping Get-ADUser to SetADUser will identify the user.
Edit: And -Manager "James" will work if "James" is the sAMAccountName of the manager.
Richard Mueller - MVP Enterprise Mobility (Identity and Access)
- Edited by Richard MuellerMVP Thursday, December 20, 2018 5:45 PM
- Proposed as answer by jrv Thursday, December 20, 2018 9:58 PM
Thursday, December 20, 2018 5:42 PM -
Get-ADUser -identity sam | Set-ADUser -identity $_ -Manager (Get-ADUser -identity <<managerSamAccount>>)
No need to use ExpandProperty to get samAccountName. Manager property expects an ADUser object, so you need to use Get-ADUser to get the manager's object
If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful. (99,108,97,121,109,97,110,50,64,110,121,99,97,112,46,114,114,46,99,111,109|%{[char]$_})-join''
No need to use "Identity" here.\_(ツ)_/
Thursday, December 20, 2018 9:59 PM -
Still does not work. With or without the -identity parameter. Getting the below error:
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:1 char:37
+ Get-ADUser sam| Set-ADUser $_ -Manager( Get-ADUser James)
+ ~~
+ CategoryInfo : InvalidData: (:) [Set-ADUser], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.SetADUser
Friday, December 21, 2018 6:02 AM -
As noted above you cannot use both the pipeline and the identity. The pipeline variable only works when using it inside of a CmdLet and not as a parameter of a pipeline command.
help pipeline
When in doubt always start by reading the help for the commands you are trying to use.
help Get-AdUser -full
help Set-AdUser -fullRead everything very carefully. If you are still confused it means you need to start with a PowerShell tutorial in order to understand how this tool works.
Microsoft Virtual Academy - Getting Started with Microsoft PowerShell
After you have learned basic PowerShell this will all be less confusing.
\_(ツ)_/
Friday, December 21, 2018 6:56 AM -
Simplest way, -identity aduser object gets passed over the pipe by value:
Get-ADUser sam | Set-ADUser -Manager James
You could do it this way if you want. $_ must be inside a script block.
Get-ADUser -identity sam | Select-Object -ExpandProperty samaccountname |
foreach-object { Set-ADUser -identity $_ -Manager 'James' }
or even this, since -identity accepts pipeline input, you can use a script block with it:
Get-ADUser -identity sam | Select-Object -ExpandProperty samaccountname |
Set-ADUser -identity { $_ } -Manager 'James'Friday, December 21, 2018 2:32 PM -
A simple demonstration of how a pipeline works.
We create a CSV with only the samname of a few users then import it into the pipeline to a new command:
get-aduser -Filter * -ResultSetSize 5 | select SamAccountName | Export-Csv users.csv -NoType import-Csv users.csv | Get-ADPrincipalGroupMembership
Try it to see how it works.
\_(ツ)_/
Friday, December 21, 2018 3:06 PM -
Hi,
Thanks for your question.
Get-ADUser sam| Set-ADUser -Manager ( Get-ADUser James)
You need to learn more about the pipeline from Jrv's links.
Best Regards,
Lee
Just do it.
Monday, December 24, 2018 7:41 AM -
Thanks it works.Monday, December 24, 2018 8:23 AM