# One script to handle CSV File or array

**URL:** <https://forums.powershell.org/t/one-script-to-handle-csv-file-or-array/18059>\
**Category:** PowerShell Help\
**Created:** [November 27, 2021, 12:12am UTC](https://forums.powershell.org/t/one-script-to-handle-csv-file-or-array/18059 "2021-11-27T00:12:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jhill](https://avatars.discourse-cdn.com/v4/letter/j/d78d45/32.png) [@jhill](https://forums.powershell.org/u/jhill)\
**Post date:** [November 27, 2021, 12:12am UTC](https://forums.powershell.org/t/one-script-to-handle-csv-file-or-array/18059/1 "2021-11-27T00:12:35Z")

</div>

TL;DR: is it possible to send a pipeline to a script from a script?

When users call my script, I’d like to give them the option to specify either a CSV file with the various parameters defined, OR let them specify the parameters directly. If the latter, the parameters would be submitted to the pipeline.

For example, if mycsv.csv contains:  
Name, Rank  
Jon, Sergeant  
Jane, Lieutenant

then ./myscript.ps1 -CSVFile .\mycsv.csv should process Jon and Jane.

Alternately, if someone wants to specify the Name and Rank values explicitly, they could do something like:  
(@{Name=Jon; Rank=Sergeant), @{Name=‘Jane’; Rank=‘Lieutenant’}) | .\myscript.ps1

What I have right now is:

```auto
[CmdletBinding(DefaultParameterSetName="Pipeline")] 
param( 
    [parameter(Mandatory=$true,ParameterSetName="FromCSV",ValueFromPipeline=$false)] 
        [String]$CSVFile,
    [parameter(Mandatory=$true,ParameterSetName="Pipeline", ValueFromPipelineByPropertyName = $true)]
        [String] $Name, 
    [parameter(Mandatory=$false,ParameterSetName="Pipeline", ValueFromPipelineByPropertyName = $true)]
        [String] $Rank
) 

 begin { 
    $ParamSetName = $PSCmdlet.ParameterSetName 
    if ($ParamSetName -eq 'FromCSV') {
        $scriptName = '\\fullpath\mscript.ps1'
        get-item $scriptName #I just do this to confirm that the file exists and is accessible
        $values = import-csv -path $CSVFile;
        $values | $scriptname #THIS FAILS
        exit 0;
    }
process { 
    write-host $Name, $Rank;
}

```

As you can see, I’m trying to send the contents of the CSV into the pipeline of the same script, but I keep getting errors that the ps1 file doesn’t exist or that I can’t have an expression at the end of a pipe.

```auto
$values | $scriptname
$values | Invoke-Expression $ScriptName
$values | (Invoke-Expression $ScriptName)

```

OTOH, the following does execute, so maybe I should stop being clever with pipelines?

```auto
        $values | foreach-object {
            $cmd = "$scriptName -Name ""$($_.Name)"" -Rank ""$($_.Rank)"""
            invoke-expression $cmd;
        }

```

Advice welcomed. For now I’ll just use the foreach-object.

Thanks.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [November 27, 2021, 1:31am UTC](https://forums.powershell.org/t/one-script-to-handle-csv-file-or-array/18059/2 "2021-11-27T01:31:00Z")

</div>

> [@jhill](#):
>
> When users call my script, I’d like to give them the option to specify either a CSV file with the various parameters defined, OR let them specify the parameters directly.

I’d expect users clever enough to use a command line interface like PowerShell with a script to be clever enough as well to do it the proper PowerShell way. 😉

… something like this:

```auto
Import-Csv -Path .\mycsv.csv | .\myscript.ps1

```

I’d rather create a function instead of a script actually to make the experience even more “_powershelly_”.

---

<div class="post-metadata">

**Author:** ![jhill](https://avatars.discourse-cdn.com/v4/letter/j/d78d45/32.png) [@jhill](https://forums.powershell.org/u/jhill)\
**Post date:** [November 27, 2021, 10:35pm UTC](https://forums.powershell.org/t/one-script-to-handle-csv-file-or-array/18059/3 "2021-11-27T22:35:36Z")

</div>

I finally figured it out.

```auto
$values | .($scriptname)

```

I’m not entirely sure what the dot is doing there syntactically - it’s not dot sourcing because there’s no space - but it worked.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [November 27, 2021, 11:04pm UTC](https://forums.powershell.org/t/one-script-to-handle-csv-file-or-array/18059/4 "2021-11-27T23:04:05Z")

</div>

> [@jhill](#):
>
> I finally figured it out.

Great that you’ve found a solution and thanks for sharing. 👍🏽 🤘🏽 🙂

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:20pm UTC](https://forums.powershell.org/t/one-script-to-handle-csv-file-or-array/18059/5 "2024-05-16T20:20:10Z")

</div>

Closed on May 16, 2024, 8:20pm UTC


