Get all disable accounts from Active Directory using PowerShell script export in CSV file
To export all disabled user accounts from Active Directory to a CSV file using PowerShell, you can modify the previous script to include exporting the results to a CSV file. Here's how you can do it:
# Import the Active Directory module
Import-Module ActiveDirectory # Get all disabled user accounts from Active Directory $disabledUsers = Get-ADUser -Filter {Enabled -eq $false} -Properties Name, SamAccountName, UserPrincipalName # Specify the path for the CSV file $csvFilePath = "C:\Path\To\DisabledUsers.csv" # Export disabled user accounts to CSV file $disabledUsers | Select-Object Name, SamAccountName, UserPrincipalName | Export-Csv -Path $csvFilePath -NoTypeInformation Write-Host "Disabled user accounts exported to $csvFilePath"
This script adds the following steps:
Specifies the path where you want to save the CSV file using the
$csvFilePath
variable. Make sure to replace"C:\Path\To\DisabledUsers.csv"
with the desired file path.Uses the
Export-Csv
cmdlet to export the disabled user accounts to the CSV file specified by$csvFilePath
.The
-NoTypeInformation
parameter ensures that the CSV file does not include the type information in the header.Writes a message to the console indicating that the disabled user accounts have been exported to the CSV file.
When you run this script, it will retrieve all disabled user accounts from Active Directory, export them to a CSV file specified by $csvFilePath
, and display a message confirming the export.
Comments