Answered by:
best way to export informations in Powershell, newbee question

Question
-
hi all
from time to time we need adhoc information about our AD, OU, Exchange, Groups or what else for statistics or other systems.
example to create import-file with information for other system (please ignor erros) adhoc code on my MAC
$groups = get-adgroupmember "groupname" foreach($group in $groups){ $dist = $group.distributionsname $aduser = get-aduser "abc"+$dist+";"+$aduser.name+";"+"InformationForInput" etc etc ... >>adhoc.txt }
you see, output is typical VBA and not good to read
thanks for Tipps
Sunday, February 16, 2020 7:25 PM
Answers
-
Something like this may work for you (Note: the code is untested):
$groupname = <name-of-the-group> get-adgroupmember $groupname | foreach{ Try{ $u = Get-ADUser $_ -ErrorAction STOP # the group may contain other groups, contacts, etc. [PSCustomObject]@{ GroupName=$groupname UserName=$u.name etc. etc. } } Catch{} # do nothing } Export-CSV <file-name> -NoTypeInfo
--- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)
- Marked as answer by Dont - Worry Monday, February 17, 2020 5:07 PM
Sunday, February 16, 2020 8:40 PM -
RIch's code has a bad syntax - missing a pipe. THis is why I prefer the following style for PS code. It makes these things easier to see.
$groupname = 'name-of-the-group' $csvfile = 'filename here' Get-AdgroupMember $groupname | Where-Object{ $_.ObjectClass -eq 'User' } | ForEach-Object{ $u = Get-ADUser $_ [PSCustomObject]@{ GroupName=$groupname UserName=$u.name } } | Export-CSV $csvfile -NoTypeInfo
\_(ツ)_/
- Edited by jrv Monday, February 17, 2020 6:27 PM
- Marked as answer by Dont - Worry Tuesday, February 18, 2020 6:41 PM
Monday, February 17, 2020 6:26 PM
All replies
-
Something like this may work for you (Note: the code is untested):
$groupname = <name-of-the-group> get-adgroupmember $groupname | foreach{ Try{ $u = Get-ADUser $_ -ErrorAction STOP # the group may contain other groups, contacts, etc. [PSCustomObject]@{ GroupName=$groupname UserName=$u.name etc. etc. } } Catch{} # do nothing } Export-CSV <file-name> -NoTypeInfo
--- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)
- Marked as answer by Dont - Worry Monday, February 17, 2020 5:07 PM
Sunday, February 16, 2020 8:40 PM -
thanks Rich
found also another solution, what do you think?
$output += $groupname $output += $u.name $output += "STRING" etc. etc. $output | export-csv ...
both better to read as :-)
"abc"+$dist+";"+$aduser.name+";"+...
Monday, February 17, 2020 5:11 PM -
thanks Rich
found also another solution, what do you think?
$output += $groupname $output += $u.name $output += "STRING" etc. etc. $output | export-csv ...
both better to read as :-)
"abc"+$dist+";"+$aduser.name+";"+...
PsCustomObject is the best way to do this.
\_(ツ)_/
Monday, February 17, 2020 6:22 PM -
RIch's code has a bad syntax - missing a pipe. THis is why I prefer the following style for PS code. It makes these things easier to see.
$groupname = 'name-of-the-group' $csvfile = 'filename here' Get-AdgroupMember $groupname | Where-Object{ $_.ObjectClass -eq 'User' } | ForEach-Object{ $u = Get-ADUser $_ [PSCustomObject]@{ GroupName=$groupname UserName=$u.name } } | Export-CSV $csvfile -NoTypeInfo
\_(ツ)_/
- Edited by jrv Monday, February 17, 2020 6:27 PM
- Marked as answer by Dont - Worry Tuesday, February 18, 2020 6:41 PM
Monday, February 17, 2020 6:26 PM