Change directory and file name;
# Set the location where the log file will be written
$OUTPUT_DIRECTORY = "Your directory"
$OUTPUT_FILENAME = "filepath"
$OUTPUT_PATH = Join-Path -Path $OUTPUT_DIRECTORY -ChildPath $OUTPUT_FILENAME
# Remove the existing output file if it exists
If (Test-Path -Path $OUTPUT_PATH) { Remove-Item -Path $OUTPUT_FILENAME -ErrorAction SilentlyContinue }
# USER_CONTEXT: Gets all Active Directory users and their properties.
Get-ADUser -Filter * -properties samAccountName | % { Get-ADUser $_.SamAccountName -properties * | ConvertTo-JSON -compress | Out-File -encoding utf8 $OUTPUT_PATH -Append }
# ASSET_CONTEXT: Gets all Active Directory assets and their properties.
Get-ADComputer -Filter * -properties samAccountName | % { Get-ADComputer $_.SamAccountName -properties * | ConvertTo-JSON -compress | Out-File -encoding utf8 $OUTPUT_PATH -Append }