Project 1 audit user

This commit is contained in:
2026-01-26 08:39:36 +01:00
parent 3e23329086
commit 20dbbbdd77
5 changed files with 104 additions and 2 deletions

View File

@@ -0,0 +1,28 @@
# PowerShell User Audit Script
# Get all local usernames as strings
#! Get-LocalUser returns objects, not just strings.
#! -split $users is incorrect for objects.
$users = Get-LocalUser | Select-Object -ExpandProperty Name
foreach ($username in $users) {
$badChar = $false
# Check each character
foreach ($char in $username.ToCharArray()) {
if ($char -notmatch "[A-Za-z]") {
$badChar = $true
break
}
}
# Check first character
$firstChar = $username[0]
$validFirstLetter = $firstChar -match "[A-Za-z]"
# Flag invalid usernames
# * Note: -ge 20 includes length 20. If you want max length 20 valid, use -gt 20.
if ($badChar -or $username.Length -gt 20 -or -not $validFirstLetter) {
Write-Host "The username '$username' is invalid"
}
}