Update AD Scripts/AD - Get Computer's Local Users.ps1

This commit is contained in:
2025-08-07 20:35:55 +00:00
parent b83a0f6f30
commit ae33c3abfc
@@ -0,0 +1,43 @@
<#
Author: Gabe Kerntke
Date: 4/8/24
.Requirements
Needs to be run on a Domain Controller or where the "Active Directory module for Windows Powershell" Feature is installed. Also
Need to be running under credentials with Domain Admin rights.
.Synopsis
Script will query all computers in the domain for local users.
#>
#Import Module
Import-module ActiveDirectory
#Set default error action
$ErrorActionPreference = "SilentlyContinue"
#Variables for paths
$OutputDirectory = "C:\Temp\Script Cache"
$CSVOutput = "$OutputDirectory\Local_Users.csv"
#Variable to find all AD computers
$Computers = Get-ADComputer -filter * | Select-Object -ExpandProperty Name
#Region Script Start
#Create output directory if it doesn't exist
If ((Test-Path $OutputDirectory) -eq $False) { New-Item -ItemType directory -Path $OutputDirectory }
#Remove existing CSV output file to prevent issues
If ((Test-Path $CSVOutput -ErrorAction SilentlyContinue) -eq $True) { Remove-Item $CSVOutput -Force }
foreach ($Computer in $Computers) {
$Computer | Foreach-Object {
If ((Test-Connection $Computer -Quiet) -eq $true) {
Get-WmiObject -ComputerName $Computer -Class Win32_UserAccount -Filter "LocalAccount=True" | Select-Object PSComputerName, Name, Disabled
}
} | Export-Csv -Path $CSVOutput -Append -NoType
}
Write-host "Script Output: $CSVOutput"
#Region Script End