0

I am relatively new to scripting and have been trying to solve a particular script problem in powershell, I am having difficult in exporting the output to a csv file, the script runs fine when displaying to the console but i either get string lengths or content relaring to the script. I am missing something with changing properties to content but cant seem to workout how to do it Many thanks in anticipation

import-csv "C:\scripts\test1\data-Raw.csv"|
foreach{ 
if ($_.Hide -eq 'True') { @{OutFinal=$_.OT_Pre,$_.extension}}
if ($_.Hide -eq 'False') { @{OutFinal=$_.extension,$_.extension}}
} | Export-Csv -NoTypeInformation "C:\scripts\test1\data-2.csv"
0

1 Answer 1

2

try to convert in pscustomobject like this

import-csv "C:\scripts\test1\data-Raw.csv"|
foreach{ 
if ($_.Hide -eq 'True') { [pscustomobject]@{OutFinal=$_.OT_Pre;Extension=$_.extension}}
else { [pscustomobject]@{OutFinal=$_.extension;Extension=$_.extension}}
} | Export-Csv -NoTypeInformation "C:\scripts\test1\data-2.csv"

or simply like it:

import-csv "C:\scripts\test1\data-Raw.csv"|
foreach{
    [pscustomobject]@{
    OutFinal=if ($_.Hide -eq 'True'){$_.OT_Pre} else {$_.extension}
    Extension=$_.extension
    } 
} | Export-Csv -NoTypeInformation "C:\scripts\test1\data-2.csv"

if you want keep all property and add property you can do like it:

import-csv "C:\scripts\test1\data-Raw.csv"|
    select *, @{Name="OutFinal"; Expression={if ($_.Hide -eq 'True'){$_.OT_Pre} else {$_.extension}}} |
        Export-Csv -NoTypeInformation "C:\scripts\test1\data-2.csv"
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks the first soltion seems to overcome my issue as a sidenote how would i include the other fields in the source csv file to the output i have tried combinations of $_. etc but i feel i dont fully understand this component in scripting yet

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.