PowerShell - Get all AD Users Account creation and changed dates
- Jon Boyette
- Jan 10, 2022
- 1 min read
Save as a .ps1 and run, outputs all Users to c:\temp\AllADUsersCreationInfo.txt with account creation and changed info, includes if account is expired
Function Get-UserProcess {
[CmdletBinding()]
param (
[Parameter(Position=0, Mandatory = $true, HelpMessage="Provide server names", ValueFromPipeline = $true)] $Computername,
[Parameter(Position=1, Mandatory = $false, HelpMessage="Provide username", ValueFromPipeline = $false)] $UserName = $env:USERNAME
)
$Array = @()
Foreach ($Comp in $Computername) {
$Comp = $Comp.Trim()
Write-Verbose "Processing $Comp"
Try{
$Procs = $null
$Procs = Invoke-Command $Comp -ErrorAction Stop -ScriptBlock{param($Username) Get-Process -IncludeUserName | Where-Object {$_.username -match $Username}} -ArgumentList $Username
If ($Procs) {
Foreach ($P in $Procs) {
$Object = $Mem = $CPU = $null
$Mem = [math]::Round($P.ws / 1mb,1)
$CPU = [math]::Round($P.CPU, 1)
$Object = New-Object PSObject -Property ([ordered]@{
"ServerName" = $Comp
"UserName" = $P.username
"ProcessName" = $P.processname
"CPU" = $CPU
"Memory(MB)" = $Mem
})
$Array += $Object
}
}
Else {
Write-Verbose "No process found for $Username on $Comp"
}
}
Catch{
Write-Verbose "Failed to query $Comp"
Continue
}
}
If ($Array) {
Return $Array
} Export-Csv c:\temp\AllADUsersCreationInfo.txt -NoTypeInformation
}
Comments