1

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
3
  • 1
    The path to your csv is unquoted and contains spaces. That is why PowerShell sees what is after the quote as second positional parameter 'Delimiter.' Tip: do not cram everything in one line of code, you will make errors you can hardly spot. Commented Aug 30, 2021 at 14:20
  • Note that you will get better performance (much better if you have more than a few hundred users) if you use Get-ADUser -Filter * -Properties Name, DistinguishedName instead 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. Commented Aug 30, 2021 at 15:02
  • "what is after the quote" should have been "what is after the space".. Commented Aug 30, 2021 at 18:08

2 Answers 2

1

The issue is in the Export-Csv portion of your command. Hard-coded paths with spaces must be surrounded by quotes, in this case, double quotes since you are relying on a sub-expression within the string (single quotes work for paths but your sub-expression would be rendered literally, and not return the result of the sub-expression):

Export-Csv "C:\Users\SystemName\Desktop\Export\Domain Users\Output Folder\$((Get-Date).ToString("yyyyMMdd_HHmmss"))_DomainUsers.csv"

Basically, move the quote right before $( to the start of the path.


Additional Info

It complains about the Delimeter parameter because when you did not wrap your path with quotes, it assumed the next part of the string was a positional parameter to Export-Csv. The second positional argument of Export-Csv binds to the -Delimter named-parameter, hence why this was mentioned though you seemingly didn't invoke the -Delimeter parameter by name.

Sign up to request clarification or add additional context in comments.

Comments

0

Your export command should look like this

Export-Csv "C:\Users\SystemName\Desktop\Export\Domain Users\Output Folder $((Get-Date).ToString('yyyyMMdd_HHmmss'))_DomainUsers.csv" -NoTypeInformation

If you use double quotes, then you have to use single quotes to quote anything inside the double quotes:

"this is an example 'of how' it should be"

if you do this

"this is an example "of how" it should NOT be"

The powershell sees it like 2 different strings.

Hope that makes sense.

1 Comment

You do not have to escape the quotes if the same quotes occur again within the sub-expression, only if you need to use the same quoting type within the literal portion of the string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.