2

I am trying to run a get-aduser command and | it into a set-aduser command, but how do I display the get-aduser command and also pipe it into the set-aduser?

Thanks

mklement0
  • 382,024
  • 64
  • 607
  • 775
Cory
  • 21
  • 2
  • Save it to a variable. Display the variable, and then pipe the variable to set-aduser. – Joel Coehoorn Apr 06 '22 at 16:00
  • 3
    `Set-ADUser` has a `-PassThru` switch to see the object that was worked on. – Abraham Zinala Apr 06 '22 at 16:27
  • 2
    @AbrahamZinala - which may not be what the user wants to display; as I read the question - which could be clarified - the user wants to see what `Get-ADUser` returns, then modify it with `Set-ADUser`. Using `-PassThru` on `Set-ADUser` will show the modified object. – Jeff Zeitlin Apr 06 '22 at 17:03
  • 1
    Cory, you should edit this question to clarify your intent. If you want to show the object as it is when you get it, my solution below should serve. If you want to show the object as you've modified it with `Set-ADUser`, take note of @AbrahamZinala comment above. – Jeff Zeitlin Apr 06 '22 at 17:05
  • @JeffZeitlin, based on the title and the wording in the question, I'd say the problem you've solved is the right one, but that makes it a duplicate of the linked post. – mklement0 Apr 06 '22 at 18:47
  • 1
    @mklement0n - yeah, and I thought of `Tee-Object` first, but didn't think to see if that question had been asked. I think I'm going to take the `Tee-Host` from the linked duplicate and add it to my General-Utilities module... – Jeff Zeitlin Apr 06 '22 at 18:49

1 Answers1

2

Pipe the output of the Get-ADUser command to a scriptblock that displays it and also executes the Set-ADUser command:

Get-ADUser ... | ForEach-Object { $_ ; Set-ADUser $_ ... }

The first $_ displays the object returned by Get-ADUser; the second passes it to the Set-ADUser command.

Jeff Zeitlin
  • 9,773
  • 2
  • 21
  • 33
  • Nice, though for robustness as a more general pattern I suggest `$_ | Set-AdUser ...` Worth pointing out that this won't scale well for commands that output many objects, because the per-object invocation of the target command can get expensive. – mklement0 Apr 06 '22 at 18:45
  • I'm having a hard time understanding how this works, pipping to a script block like shown in the answer isn't parser error? Was this meant to be `| & { process { $_ .... } }` ? cc @mklement0 hope you can help me understand – Santiago Squarzon Apr 07 '22 at 01:16
  • 2
    @Santiago Oops! I think Jeff simply forgot `%` / `ForEach-Object` before the script block - an omission I didn't notice either. – mklement0 Apr 07 '22 at 01:32
  • thanks @mklement0 ‍♂️, thought I was going mad lol a process script block would be more memory friendly and efficient here as zett42 demonstrated in the linked answer. – Santiago Squarzon Apr 07 '22 at 01:41
  • Yeah, there indeed should have been a `ForEach-Object` there; not sure how I blew that one... Added. – Jeff Zeitlin Apr 07 '22 at 10:42