question

EricOrosz-1454 avatar image
0 Votes"
EricOrosz-1454 asked RichMatheisen-8856 commented

AD Groups Powershell

I would like to pull a list of user groups using the following code`(Get-ADPrincipalGroupMembership 'username' | select -expand name | sort) | format-table -Property name` then out-file it to a .txt file but before doing that I would also like to pull out the groups from the same list that start with 'DMS' in the name in a different .txt or the same one in as a two column format if possible.

windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered RichMatheisen-8856 commented

In simpler terms, you want a list of all the groups in which a user ("username") is a member. Is that right? Then put those names into two files, one for all groups whose name begins with "DMS" and another one for all groups show names don't begin with "DMS". Is that also right?

Something like this might be what you want (NOTE: I haven't run this!):

 $dms = @()
 $username = 'xyz'
 $dmsfile = c:\junk\dmsgroups.txt
 $nondmsfile = c:\junk\nondmsgroups.txt
    
 Get-ADPrincipalGroupMembership -Identity $username |
     Select-Object -Property name |
         Sort-Object |
             ForEach-Object{
                 if ($_.name -like 'DMS*'){
                     $dms += $_.name
                 }
                 else{
                     $_.name
                 }
             } | Out-File $nondmsfile
 $dms | Out-File $dmsfile
· 8
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

The script works but just curious is there a to get the .txt to autoname as the username

0 Votes 0 ·

Hmmm . . . I thought I answered this but I don't see my reply! Let's try again. :-)

You can change lines 3 and 4 in my example to something like this:


 $dmsfile = c:\junk\$username-dmsgroups.txt
  $nondmsfile = c:\junk\$username-nondmsgroups.txt
0 Votes 0 ·

The $username-filename.txt works great but for some reason the groups are not sorted alphabetically in the out-files. What would I change in the code to achieve this and getting "Select -expand Name | sort) -join '; '" I tried adding the -descending after Sort-Object but that didn't work.
$dms = @() $username = '' $dmsfile = "\\servername\tech\IT Administration\User Administration\NewUserForms\NewUserADGroups\$username-dmsgroups.txt" $nondmsfile = "\\servername\tech\IT Administration\User Administration\NewUserForms\NewUserADGroups\$username-nondmsgroups.txt" Get-ADPrincipalGroupMembership -Identity $username | Select-Object -Property name | Sort-Object -Descending | ForEach-Object{ if ($_.name -like 'DMS*'){ $dms += $_.name } else{ $_.name } } | Out-File $nondmsfile $dms | Out-File $dmsfile

0 Votes 0 ·
Show more comments
EricOrosz-1454 avatar image
0 Votes"
EricOrosz-1454 answered RichMatheisen-8856 commented

I had another question when creating a new user account in AD for the home folder creation is there a powershell script that I could run that after it creates the new user folder on the lets say H drive where it creates a subfolder in any new user folder called Recover?

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

If you've scripted the creation of the new user(s) you can create the folder and the subfolder yourself.

In any case, that's a completely different topic and you should ask about it in a new post with an appropriate subject.

If the script answered your question, mark it as "answered".

0 Votes 0 ·