<# .SYNOPSIS Retrieves the group memberships of specified Active Directory computer accounts. .DESCRIPTION This script reads a list of server names from a text file and uses the Get-ADPrincipalGroupMembership cmdlet to retrieve the groups that each computer account is a member of in Active Directory. The results are displayed in the console. .EXAMPLE .\Get-ADPrincipalGroup.ps1 This command runs the script, which reads server names from "C:\servers.txt" and outputs the group memberships for each server to the console. .EXAMPLE Get-ADPrincipalGroup.ps1 | Out-File -FilePath "ServerGroupMemberships.txt" This command runs the script and saves the output to a text file named "ServerGroupMemberships.txt" in the current directory. .INPUTS None. This script does not accept any input from the pipeline. .OUTPUTS The script outputs the group memberships of each specified computer account, including the server name and the groups it belongs to. .NOTES Ensure that the Active Directory module is installed and that you have the necessary permissions to query group memberships in your AD environment. The script assumes that the server names are listed in a text file at "C:\servers.txt", with one server name per line. Adjust the file path as needed for your environment. #> $cred = Get-Credential -Message "Enter Domain Admin credentials" $script = { Import-Module ActiveDirectory # Server prefixes to target removal $prefixes = @("PRE1", "PRE2", "PRE3", "PRE4") #Enter whatever is the location desigination for the servers you want to target. I use this for filtering out test/dev servers and only targeting production servers in specific OUs since the first two characters indicate the location. This is all assuming your server names are standardized with a prefix that indicates their location or environment. Adjust the prefixes as needed for your environment. $results = @() foreach ($prefix in $prefixes) { $computers = Get-ADComputer -Filter "Name -like '$prefix*'" -Properties Name, OperatingSystem, LastLogonDate foreach ($computer in $computers) { $results += [PSCustomObject]@{ Name = $computer.Name OperatingSystem = $computer.OperatingSystem LastLogonDate = $computer.LastLogonDate } } } $results | Export-Csv -Path "FilteredComputers.csv" -NoTypeInformation } # Save to a temporary file $tempScriptPath = "$env:TEMP\ComputerExportScript.ps1" $script | Out-File -FilePath $tempScriptPath # Launch new PowerShell session with elevated credentials Start-Process powershell.exe -Credential $cred -ArgumentList "-ExecutionPolicy Bypass -File `"$tempScriptPath`""