I am getting the following error while executing this powershell script. Please correct it and help me with the right one.
Powershell Script I am Running :
Get-ADUser -filter * -properties * | select Name, @{l='OU';e={$_.DistinguishedName.split(',')[1].split('=')[1]}} | Export-Csv C:\Users\SystemName\Desktop\Export\Domain Users\Output Folder\"$((Get-Date).ToString("yyyyMMdd_HHmmss"))_DomainUsers.csv" -NoTypeInformation
Error I receive :
Export-Csv : Cannot bind parameter 'Delimiter'. Cannot convert value "Users\Output" to type "System.Char". Error: "String must be exactly one character long."
At C:\Users\SystemName\Desktop\Export\Domain Users\DomainUsers.ps1:42 char:170
+ ... t-Csv C:\Users\SystemName\Desktop\Export\Domain Users\DomainUsers\Output Folder\ ...
+ ~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Export-Csv], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.ExportCsvCommand
Get-ADUser -Filter * -Properties Name, DistinguishedNameinstead of-Properties *. There is a lot of data in Active Directory that you're telling the domain controller to send you, and then you're immediately throwing it away. Also, you should be aware that the way you're parsing the distinguished name isn't very robust. If you need a better way, try this.