Combining values (tokens) in Powershell

I have a csv file with 3 columns and multiple rows as shown below

All     Year1     2018

All     Year2     2019

For each row I would like to get the below output

 Set_this_value_to All Year1=2018

 Set_this_value_to All Year2=2019

I tried something like this

Get-Content filename.csv | ForEach-Object {
"Set_this_value_to" + " " + $_.split(",")

But the output I get is:

Set_this_value_to ALL Year1 2018
Set_this_value_to ALL Year2 2019

Thanks in advance

  • Since you are sound fairly new to PS, it's vital you get some training to avoid confusion / frustration, etc.


    There are plenty of them same on this forum. Use the searchbox and search for learn PowerShell

    Use the Import-Csv cmdlet instead of Get-Content.

    Get-Content just read in the data as data rows, there are other options.

    Import-Csv is specifically for dealing with such files.

        # Get parameters, examples, full and Online help for a cmdlet or function

        (Get-Command -Name Import-Csv).Parameters
        Get-help -Name Import-Csv -Examples
        Get-help -Name Import-Csv -Full
        Get-help -Name Import-Csv -Online

        Get-Help about_*
        Get-Help about_Functions

        # Find all cmdlets / functions with a target parameter
        Get-Help * -Parameter Append

        # All Help topics locations
        explorer "$pshome\$($Host.CurrentCulture.Name)"

    See the examples from the above...

    Import-Csv  has a -Delimiter switch that allows you to specify what to use as the column separator.