Here's a PowerShell script that pulls AD user objects, pipes names to fzf, and shows a preview of important attributes:
powershell Copy code
# Requires the ActiveDirectory module
Import-Module ActiveDirectory
# Create a temporary file for the preview script
$previewScript = New-TemporaryFile
$previewScriptPath = $previewScript.FullName + ".ps1"
Rename-Item -Path $previewScript.FullName -NewName $previewScriptPath
# Write the preview script content
@"
param([string]`$username)
Import-Module ActiveDirectory
`$user = Get-ADUser -Identity `$username -Properties DisplayName, EmailAddress, Title, Department, Manager, Enabled, LockedOut, PasswordExpired, PasswordLastSet, LastLogonDate, whenCreated, Office, OfficePhone, MobilePhone
Write-Output "Display Name: `$(`$user.DisplayName)"
Write-Output "Email: `$(`$user.EmailAddress)"
Write-Output "Title: `$(`$user.Title)"
Write-Output "Department: `$(`$user.Department)"
Write-Output "Office: `$(`$user.Office)"
Write-Output "Office Phone: `$(`$user.OfficePhone)"
Write-Output "Mobile: `$(`$user.MobilePhone)"
Write-Output "Manager: `$(if(`$user.Manager){(Get-ADUser -Identity `$user.Manager).Name}else{'N/A'})"
Write-Output "Account Enabled: `$(`$user.Enabled)"
Write-Output "Account Locked: `$(`$user.LockedOut)"
Write-Output "Password Expired: `$(`$user.PasswordExpired)"
Write-Output "Password Last Set: `$(`$user.PasswordLastSet)"
Write-Output "Last Logon: `$(`$user.LastLogonDate)"
Write-Output "Created: `$(`$user.whenCreated)"
"@ | Out-File -FilePath $previewScriptPath
# Get all AD users and pipe to fzf
try {
$selectedUser = Get-ADUser -Filter * |
Select-Object -ExpandProperty SamAccountName |
Out-String -Stream |
Where-Object { $_ -ne "" } |
& fzf --preview "powershell -File $previewScriptPath {}"
if ($selectedUser) {
Write-Host "Selected user: $selectedUser"
# You can add additional actions here for the selected user
}
}
catch {
Write-Error "An error occurred: $_"
}
finally {
# Clean up the temporary preview script
if (Test-Path $previewScriptPath) {
Remove-Item -Path $previewScriptPath -Force
}
}