Forum Discussion
gahenry
Sep 12, 2023Copper Contributor
How to remove ".." in csv
Hello I am pretty green at this so please forgive me if this is a real stupid question... I need to run a SQLCMD on a server 2019 machine and export results to a CSV. I have tried to format...
gahenry
Sep 14, 2023Copper Contributor
Hi and thank you for your help and guidance. When I applied your recommendations I got this error...
It complained that encoding was not specified so I was not sure what to use so I chose ASCII...
can you help me understand what I did wrong?
Don't I Have to define the variable $newCSV somehow??
----
PS Invoke-Sqlcmd -Server 192.168.0.4 -Username PAReadonly2 -Password "<password>" -Database lc_V3_acephoto_net -InputFile "C:\Users\Amber\Documents\SQL Server Management Studio\MPNsimple.sql" |
>> ConvertTo-Csv -NoTypeInformation |
>> ForEach-Object { $_.Replace("""", "") } |
>> Out-File -encoding ASCII -FilePath "c:\junk\" $NewCSV;
Out-File : A positional parameter cannot be found that accepts argument '$null'.
At line:4 char:17
+ ... Out-File -encoding ASCII -FilePath "c:\junk\" $NewCSV;
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Out-File], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.OutFileCommand
thanks for any additional helpyou can provide
Gary
It complained that encoding was not specified so I was not sure what to use so I chose ASCII...
can you help me understand what I did wrong?
Don't I Have to define the variable $newCSV somehow??
----
PS Invoke-Sqlcmd -Server 192.168.0.4 -Username PAReadonly2 -Password "<password>" -Database lc_V3_acephoto_net -InputFile "C:\Users\Amber\Documents\SQL Server Management Studio\MPNsimple.sql" |
>> ConvertTo-Csv -NoTypeInformation |
>> ForEach-Object { $_.Replace("""", "") } |
>> Out-File -encoding ASCII -FilePath "c:\junk\" $NewCSV;
Out-File : A positional parameter cannot be found that accepts argument '$null'.
At line:4 char:17
+ ... Out-File -encoding ASCII -FilePath "c:\junk\" $NewCSV;
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Out-File], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.OutFileCommand
thanks for any additional helpyou can provide
Gary
LainRobertson
Sep 14, 2023Silver Contributor
Hi, Gary.
If this is under Windows PowerShell 5.1 then I'm not sure why it's complaining about needing the -Encoding parameter, as that's not something I've ever needed to be provided. Still, that's not the issue now.
The issue is what you've done with the final line, which currently looks like this:
Out-File -encoding ASCII -FilePath "c:\junk\" $NewCSV;
The issue is the space between "c:\junk\" and $NewCSV, as that's not a valid filename. Secondly, if you haven't given $NewCSV a value earlier on then that's probably also why the error mentioned being unable to use a "$null" value.
The last line should look like any of the following:
# Option 1: This will only work if you've assigned a name (excluding the path to the file) to $NewCSV beforehand.
Out-File -Encoding ASCII -FilePath "c:\junk\$NewCSV";
# Option 2: Specify the full filename manually, removing $NewCSV entirely.
Out-File -Encoding ASCII -FilePath "c:\junk\test.csv";
# Option 3: This will work whether you assign just a filename or the full path (including the filename).
Out-File -Encoding ASCII -FilePath $NewCSV;
Cheers,
Lain
- gahenrySep 18, 2023Copper Contributorthanks everyone for your help.