# Store a string array in a variable and pass it to a command

**URL:** <https://forums.powershell.org/t/store-a-string-array-in-a-variable-and-pass-it-to-a-command/13332>\
**Category:** PowerShell Help\
**Created:** [October 24, 2019, 1:06pm UTC](https://forums.powershell.org/t/store-a-string-array-in-a-variable-and-pass-it-to-a-command/13332 "2019-10-24T13:06:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![merlin](https://avatars.discourse-cdn.com/v4/letter/m/8baadc/32.png) [@merlin](https://forums.powershell.org/u/merlin)\
**Post date:** [October 24, 2019, 1:06pm UTC](https://forums.powershell.org/t/store-a-string-array-in-a-variable-and-pass-it-to-a-command/13332/1 "2019-10-24T13:06:26Z")

</div>

The following code works very well

[pre] $Include = “\*.exe, \*.rpt, \*.chm”

Copy-Item -Include $Include -Path $NetPath -Recurse -Force -Destination $TmpPath [/pre]

Now I would like to pass the entire copy command to another command. I do this as follows:

[pre] $Include = “\*.exe, \*.rpt, \*.chm”

$Command = "Copy-Item -Include $Include -Path $NetPath -Recurse -Force -Destination $TmpPath”

Start-Process powershell.exe -Verb runas -ArgumentList “$Command” [/pre]

Apparently, the string array in variable $Include is no longer recognized. What do I have to change for the code to work?

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [October 24, 2019, 3:01pm UTC](https://forums.powershell.org/t/store-a-string-array-in-a-variable-and-pass-it-to-a-command/13332/2 "2019-10-24T15:01:24Z")

</div>

Two things:

1. The $Include variable is not a string array, it's a just a string. See below:
```
PS C:\Users\rasim\Desktop> $Include = "*.exe, *.rpt, *.chm"

$Include.GetType()

IsPublic IsSerial Name BaseType                                                                                                                                                                                              
-------- -------- ---- --------                                                                                                                                                                                              
True True String System.Object                                                                                                                                                                                         

PS C:\Users\rasim\Desktop> $Include = "*.exe,", "*.rpt", "*.chm"

$Include.GetType()

IsPublic IsSerial Name BaseType                                                                                                                                                                                              
-------- -------- ---- --------                                                                                                                                                                                              
True True Object[] System.Array
```

```
    </li>
    <li> Not sure why you are starting a Powershell in Powershell, but to answer your question you are not passing the variable to the process, so it doesn't know anything about the $Include variable or even $NetPath or $TmpPath. If you are trying to execute the command on another system, you should be using Invoke-Command and then you can use the $using. See https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_remote_variables?view=powershell-6
     
    </li>

```

---

<div class="post-metadata">

**Author:** ![merlin](https://avatars.discourse-cdn.com/v4/letter/m/8baadc/32.png) [@merlin](https://forums.powershell.org/u/merlin)\
**Post date:** [October 24, 2019, 3:22pm UTC](https://forums.powershell.org/t/store-a-string-array-in-a-variable-and-pass-it-to-a-command/13332/3 "2019-10-24T15:22:41Z")

</div>

> Not sure why you are starting a Powershell in Powershell

I want to execute the copy command with elevated right. If that goes the other way, I would be grateful for any help.

> If you are trying to execute the command on another system

The command should not be executed on another system, but with elevated privileges.

---

<div class="post-metadata">

**Author:** ![merlin](https://avatars.discourse-cdn.com/v4/letter/m/8baadc/32.png) [@merlin](https://forums.powershell.org/u/merlin)\
**Post date:** [October 25, 2019, 3:56am UTC](https://forums.powershell.org/t/store-a-string-array-in-a-variable-and-pass-it-to-a-command/13332/4 "2019-10-25T03:56:47Z")

</div>

I found the following:

If I give $Include direct in the copy command, $Include must be an System.Array

```
$Include = "*.exe", "*.rpt", "*.chm"
Copy-Item -Include $Include -Path $Source -Recurse -Force -Destination $Target
```

When I pass the copy command in another command, $Include must be an system.object

```
$Include= "*.exe, *.rpt, *.chm"
$Command = "Copy-Item -Include$Include-Path $Source -Recurse -Force -Destination '$Target'"
Start-Process powershell.exe -Verb runas -ArgumentList "$Command"
```

Both variants work now

---

<div class="post-metadata">

**Author:** ![ta11ow](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ta11ow/32/131_2.png) [@ta11ow](https://forums.powershell.org/u/ta11ow)\
**Post date:** [October 25, 2019, 10:41am UTC](https://forums.powershell.org/t/store-a-string-array-in-a-variable-and-pass-it-to-a-command/13332/5 "2019-10-25T10:41:48Z")

</div>

You can use `-Credential` to execute the copy program with a specific credential, which generally suffices for most use cases. 🙂

&nbsp;

---

<div class="post-metadata">

**Author:** ![merlin](https://avatars.discourse-cdn.com/v4/letter/m/8baadc/32.png) [@merlin](https://forums.powershell.org/u/merlin)\
**Post date:** [October 25, 2019, 12:28pm UTC](https://forums.powershell.org/t/store-a-string-array-in-a-variable-and-pass-it-to-a-command/13332/6 "2019-10-25T12:28:32Z")

</div>

I think Iwould have to know the credentials of the user. It’s not like this. I think I can’t get on with `-Credential`.

---

<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:31pm UTC](https://forums.powershell.org/t/store-a-string-array-in-a-variable-and-pass-it-to-a-command/13332/7 "2024-05-16T20:31:13Z")

</div>

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


