Using PowerShell to Determine the Total Number of Users in a Group

Listen to this Post

2025-02-16

With these steps, you can quickly provide accurate responses to management inquiries about the number of people in a security group.

Procedures:


<h1>GSEC-EXAMPLE</h1>

$groupName = "GSEC-EXAMPLE"

<h1>Get all users in the specified security group</h1>

$groupMembers = Get-ADGroupMember -Identity $groupName -Recursive | Where-Object { $_.objectClass -eq "user" }

<h1>Get the count of users</h1>

$userCount = $groupMembers.Count

<h1>Display the result</h1>

Write-Host "The number of users in the group '$groupName' is: $userCount"

What Undercode Say:

PowerShell is an incredibly powerful tool for system administrators, especially when managing Active Directory (AD) environments. The script provided above is a simple yet effective way to determine the number of users in a specific security group. This is particularly useful for auditing purposes, ensuring compliance, or simply keeping track of user distribution within an organization.

In addition to the script above, here are some other useful PowerShell commands and practices for managing AD and Azure environments:

1. List All AD Groups:

Get-ADGroup -Filter *

2. Export Group Members to a CSV:

$groupName = "GSEC-EXAMPLE"
$groupMembers = Get-ADGroupMember -Identity $groupName -Recursive | Where-Object { $_.objectClass -eq "user" }
$groupMembers | Export-Csv -Path "C:\GroupMembers.csv" -NoTypeInformation

3. Check User Account Status:

Get-ADUser -Identity "username" -Properties Enabled | Select-Object Name, Enabled

4. Create a New AD User:

New-ADUser -Name "John Doe" -GivenName "John" -Surname "Doe" -SamAccountName "jdoe" -UserPrincipalName "[email protected]" -Path "OU=Users,DC=domain,DC=com" -AccountPassword (ConvertTo-SecureString "P@ssw0rd" -AsPlainText -Force) -Enabled $true

5. Azure AD: List All Users:

Connect-AzureAD
Get-AzureADUser

6. Azure AD: Get User Group Memberships:

Get-AzureADUserMembership -ObjectId "[email protected]"

7. VMware: List All Virtual Machines:

Get-VM

8. VMware: Start a Virtual Machine:

Start-VM -Name "VMName"

9. Linux: Check Disk Usage:

df -h

10. Linux: List Running Processes:

ps aux

11. Windows: Check System Information:

[cmd]
systeminfo
[/cmd]

12. Windows: List All Services:

[cmd]
sc query
[/cmd]

13. Windows: Check Network Configuration:

[cmd]
ipconfig /all
[/cmd]

14. Linux: Check Network Configuration:

ifconfig

15. Linux: Check Open Ports:

netstat -tuln

16. Windows: Check Open Ports:

[cmd]
netstat -an
[/cmd]

17. Linux: Check System Uptime:

uptime

18. Windows: Check System Uptime:

[cmd]
systeminfo | find “System Boot Time”
[/cmd]

19. Linux: Check Memory Usage:

free -m

20. Windows: Check Memory Usage:

[cmd]
wmic OS get FreePhysicalMemory,TotalVisibleMemorySize
[/cmd]

21. Linux: Check CPU Usage:

top

22. Windows: Check CPU Usage:

[cmd]
wmic cpu get loadpercentage
[/cmd]

23. Linux: Check Logged-In Users:

who

24. Windows: Check Logged-In Users:

[cmd]
query user
[/cmd]

25. Linux: Check System Logs:

tail -f /var/log/syslog

26. Windows: Check System Logs:

[cmd]
eventvwr
[/cmd]

27. Linux: Check Disk Space:

du -sh /path/to/directory

28. Windows: Check Disk Space:

[cmd]
dir /s
[/cmd]

29. Linux: Check File Permissions:

ls -l /path/to/file

30. Windows: Check File Permissions:

[cmd]
icacls “C:\path\to\file”
[/cmd]

31. Linux: Check System Time:

date

32. Windows: Check System Time:

[cmd]
time /t
[/cmd]

33. Linux: Check System Hostname:

hostname

34. Windows: Check System Hostname:

[cmd]
hostname
[/cmd]

35. Linux: Check System Kernel Version:

uname -r

36. Windows: Check System Version:

[cmd]
ver
[/cmd]

37. Linux: Check System Architecture:

uname -m

38. Windows: Check System Architecture:

[cmd]
echo %PROCESSOR_ARCHITECTURE%
[/cmd]

39. Linux: Check System Load Average:

cat /proc/loadavg

40. Windows: Check System Load Average:

[cmd]
typeperf “\System\Processor Queue Length”
[/cmd]

41. Linux: Check System Swap Usage:

swapon -s

42. Windows: Check System Swap Usage:

[cmd]
wmic pagefile list
[/cmd]

43. Linux: Check System Temperature:

sensors

44. Windows: Check System Temperature:

[cmd]
wmic /namespace:\root\wmi PATH MSAcpi_ThermalZoneTemperature get CurrentTemperature
[/cmd]

45. Linux: Check System Uptime:

cat /proc/uptime

46. Windows: Check System Uptime:

[cmd]
systeminfo | find “System Boot Time”
[/cmd]

47. Linux: Check System Memory Usage:

cat /proc/meminfo

48. Windows: Check System Memory Usage:

[cmd]
wmic OS get FreePhysicalMemory,TotalVisibleMemorySize
[/cmd]

49. Linux: Check System CPU Usage:

mpstat

50. Windows: Check System CPU Usage:

[cmd]
wmic cpu get loadpercentage
[/cmd]

These commands and scripts are essential for any IT professional managing Windows, Linux, or hybrid environments. They provide a quick way to gather information, troubleshoot issues, and ensure systems are running smoothly. Whether you’re working with Active Directory, Azure, VMware, or just managing local systems, these commands will help you stay on top of your game.

For more advanced PowerShell scripts and Azure AD management tips, you can refer to the official Microsoft documentation: Microsoft Docs.

By mastering these commands, you can significantly improve your efficiency and effectiveness as a Technical Support Engineer or System Administrator. Keep practicing and exploring new commands to expand your skill set and stay ahead in the ever-evolving world of IT.

References:

Hackers Feeds, Undercode AIFeatured Image