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

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?

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>
    
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.

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

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

 

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.