0

I'm trying to list only unique HomeDrive for all users in a Universal Security group and remove nested groups errors. Thanks for your help. Denis

I've tried .TrimEnd(':'), can't seem to figure out where to put it

$Group = "Universal Security group"
$HomeDrive = Get-ADGroupMember $Group | `
    ForEach-Object { 
        $UserName = $_.Name
        Try {
            #$ErrorActionPreference = "Stop"
            Get-ADUser $UserName -Properties HomeDrive | Select HomeDrive
            }
        Catch {
            Write-Host "Found a nested Group."
            }
        } | Sort-Object -Property 'HomeDrive' -Unique | Format-Table -HideTableHeaders | Out-String
Write-Host "$HomeDrive" -BackgroundColor DarkRed

The script does work but some users have their homedrives listed as only F while most are listed as F:. Basically making a lot of double entries and I do want the output to be only F. Also it generates 7 spaces after the :, That's why I have the background color.

Denis
  • 3
  • 2
  • 1
    What is `$HomeDrive = $HomeDrive.Split()` supposed to do? Is `$HomeDrive` defined earlier in your script? – kuzimoto Jun 14 '19 at 17:53
  • Oh ... Please dont use Format-Table in the pipeline like that https://stackoverflow.com/questions/36358047/how-can-i-store-output-from-format-table-for-later-use/36358921#36358921 that is _part_ of your issue. – Matt Jun 14 '19 at 17:57
  • @kuzimoto - sorry was not supposed to be included in my post. I have removed it. – Denis Jun 14 '19 at 18:01
  • No problem! But looks like mhu has it already figured out. – kuzimoto Jun 14 '19 at 18:03
  • I forgot to add that I would also like to know how many times that drive letter was used. Denis – Denis Jun 14 '19 at 18:29

1 Answers1

1

Something like this:

$group = "Universal Security group"
$homeDrives = Get-ADGroupMember $Group |
    ForEach-Object {
        if ($_.ObjectClass -eq "User")
        {    
            $user = Get-ADUser $_.Name -Properties "HomeDrive"
            $homeDrive = $user.HomeDrive.Trim().TrimEnd(":")
            return $homeDrive
        }
    } | Sort-Object -Unique

foreach ($homeDrive in $homeDrives)       
{
    Write-Host "Found home drive: $homeDrive" -BackgroundColor DarkRed
}
mhu
  • 17,720
  • 10
  • 62
  • 93