Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I remove newline from a PowerShell variable

I'm trying to do some processing logic - running some commands in parallel based on the tree configuration CSV file:

Operation;Parent;Enabled;Propagated;Job_ID;Status;Started;Finished
CA1;n/a;Y;N;;;;
PROD1;n/a;Y;N;;;Y;
CON1;CA1;N;N;;;Y;
CON2;CON1;N;N;;;Y;

I load the file into the variable and then I'm trying to find the next step which needs to be processed:

$Data = Import-Csv -delimiter ";" .\config.csv
$NextStep = $Data | Select-Object -first 1 | Where-Object {$_.Started -eq ""}
$NextStepText = $NextStep.Operation | ft -autosize | out-string

The problem is that it seems like $NextStep.Operation contains new line character. When I display it I get:

PS C:\temp\SalesForce> $NextStep.operation
CA1

PS C:\temp\SalesForce> $NextStep.Operation.Contains("`n")
False

Do you know what I'm doing wrong? I would like to display the content without the "dummy" new line character which is there even if contains method is saying it is not there.

Or please advise how to do it better. I'm still learning PowerShell; so far I just google the commands, and I'm trying to put it together.

like image 817
pepco Avatar asked Dec 02 '25 01:12

pepco


1 Answers

The newline isn't in your data, it's being added by Out-String. Observe the output of the following (in particular, where you do and don't get the newline after CA1):

$Data = import-csv -delimiter ";" .\config.csv
$NextStep = $Data | select-object -first 1 | where-object {$_.Started -eq ""}
$NextStepText = $NextStep.Operation | ft -autosize | out-string
"hi"
$NextStepText
"hi"
$NextStep.Operation;
"hi"
$NextStep.Operation | ft -autosize
"hi"

You shouldn't be using Format-Table at that step (and Out-String is unnecessary in this script) if you intend to use $NextStepText for anything other than direct output later on. Consider Format-Table (or any of the Format-* cmdlets) the end of the line for usable data.

like image 145
alroc Avatar answered Dec 03 '25 16:12

alroc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!