PowerShell Script: List all Users in a Security Groups

A question that’s been popping up here and there is “How can I list all users in a security group?” This script is part of a larger script, but this part might be helpful to showcase how we can solve this using powershell.

Import-Module ActiveDirectory
$date = get-date -UFormat "%Y%m%d"
##Example ="OU=Company,DC=Cisco,DC=local"
$Groupaccountpath = "OU=Company,DC=Cisco,DC=local"
$Groups = (Get-AdGroup -filter * -SearchBase $Groupaccountpath | Where {$_.name -like "**"} | select name -ExpandProperty name)
$Table = @()
$Record = @{
  "Group Name" = ""
  "Name" = ""
  "Username" = ""

Foreach ($Group in $Groups) {
  $Arrayofmembers = Get-ADGroupMember -identity $Group -recursive | get-aduser | Where {$_.Enabled -eq $true}  | select name,samaccountname
  foreach ($Member in $Arrayofmembers) {
    $Record."Group Name" = $Group
    $Record."Name" = $Member.name
    $Record."UserName" = $Member.samaccountname
    $objRecord = New-Object PSObject -property $Record
    $Table += $objrecord
$Table | export-csv "C:\temp\SecurityGroups_$date.csv" -NoTypeInformation

PowerShell ScriptFind Largest (Or Smallest) Files In A Directory Or Drive With PowerShell

Here’s another quick tip for you. This is a simple script that sorts the largest files in a directory. We can take out the -Descending switch to change it to smallest.

# Get all files sorted by size.
Get-ChildItem -Path 'C:\SomeFolder' -Recurse -Force -File | Select-Object -Property FullName,@{Name='SizeGB';Expression={$_.Length / 1GB}},@{Name='SizeMB';Expression={$_.Length / 1MB}},@{Name='SizeKB';Expression={$_.Length / 1KB}} | Sort-Object { $_.SizeKB } -Descending | Out-GridView