1

I need to place System.Object[] for some columns in a csv file. I tried 3 different method but none of them are working. System.Object[] is put in by powershell when that object is empty or something.

$file = "c:\output.csv"

(gc $file) -replace "'system.object[]'", ""
[io.file]::readalltext($file).replace("'system.object[]'","")
(Get-Content $file | ForEach-Object { $_ -replace "system.object[]", "" } ) | Set-Content $file
2
  • don't understand you need to add or remove the System.Object[] text? Commented Aug 13, 2015 at 13:26
  • Remove it because users are getting confused with it. Commented Aug 13, 2015 at 13:29

3 Answers 3

1

I added following code to the variable that containing System.Object[] on output. and it's seems to be working. and now I dont have to do the replacement at file level.

"Access Code" = (@($AccessCode) | Out-String).Trim()
Sign up to request clarification or add additional context in comments.

Comments

1

The bracers and the dot ([, ], .) need all to be escaped. Furthermore remove the double quotation marks, just keep the single ones. Also think about using creplace, in case you want to work case insensitive. So the command would look like this:

(gc $file) -replace 'system\.object\[\]', ''

In case you want to write everything to a new file:

(gc $file) -replace 'system\.object\[\]', ''|out-file "test2.txt" -encoding ASCII

Comments

0

Just use Escape character

(gc $file) -replace 'system.object\[\]', ""

The characters '[' and ']' are used for Regex pattern. You must use Escape character '\' to tell Powershell that This is a regular chars

1 Comment

Thanks for the code. I think I solved the problem. but will try yours as well. Thanks again.

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.