ADuser_displayname_Memberof_Script

##ADuser_displayname_Memberof

$path = “C:\Temp\Scripts\ADUserMemberof”
$Users = Get-Content “$path\Users.txt”
$FileTime = get-date -Format “yyyyMMdd-hhmmss”
$Output = “$path\UserMember0f-$FileTime.csv”
$header = “User,Group”
Add-Content $output $header
foreach($inuser in $Users)
{

if($inuser.Length -gt 20){ $inuser = $inuser.Substring(0,20) }
try
{
$Displayname = (Get-ADUser $inuser -Properties Displayname).displayname
$ADusermems = (Get-ADUser $inuser -Properties memberof).memberof
foreach($ADusermem in $ADusermems)
{

$ADgroup = ($ADusermem.Split(“,”)[0]).Split(“=”)[1]
$data = $inuser + “,” + $Displayname + “,” + $ADgroup
Add-Content $output $data
}
}
catch
{
Write-Host $inuser
$data = $inuser + “,UserNotAvailable”
Add-Content $output $data
}
}

I’m moving this to the PowerShell help category. What’s the question and what are you trying to accomplish?

Looking at this line of code:

Needs to be updated to:

$ADusermems = Get-ADUser $inuser -Properties memberof | select -ExpandProperty memberof

I don’t know that it has to. They both do the same thing just with different syntax.

You’re right… It works fine without expanding…Thanks :slight_smile:

Well to be clear that’s what the dot syntax does: print a property’s value. Select-Object -ExpandProperty is doing the same thing.
It’s just two different ways to do the same thing.

1 Like