1

So I wanted to just copy this file into Program Files, where it needs to be like this (have to right-click, run as Administrator):

Copy-Item \\Poplar\SuperSound -Destination 'C:\Program Files\' -Force -Recurse

but I needed it in a powershell script.

The usual way I can elevate is with:

powershell -command "install_bananas.bat" -Verb runas

but when I run:

powershell -command "Copy-Item \\Zuul\IT\ffmpeg -Destination 'C:\Program Files\' -Force -Recurse" -Verb runas

...that coughed up an error:

Copy-Item : A positional parameter cannot be found that accepts argument 'runas'.

So instead I have to use -argumentlist with Start-Process:

Start-Process powershell -Verb runas -argumentlist "Copy-Item \\Poplar\SuperSound -Destination 'C:\Program Files\' -Force -Recurse" 

So I guess argumentlist is used by Start-Process only.

So what is powershell -command versus Start-Process powershell -argumentlist then and why is it running into problems with -Verb runas when it has to run a multi-argument command like:

Copy-Item A -Destination B

?

NOTE: I think its finally time to buy a Powershell book.

1

Though getting a book will give you stuff, reach to the help files first since they are free and right in front of you. ;-}

# Get specifics for a module, cmdlet, or function
(Get-Command -Name Start-Process).Parameters
(Get-Command -Name Start-Process).Parameters.Keys
# Results
<#
FilePath
ArgumentList
Credential
WorkingDirectory
LoadUserProfile
NoNewWindow
PassThru
RedirectStandardError
RedirectStandardInput
RedirectStandardOutput
Verb
WindowStyle
Wait
UseNewEnvironment
Verbose
Debug
ErrorAction
WarningAction
InformationAction
ErrorVariable
WarningVariable
InformationVariable
OutVariable
OutBuffer
PipelineVariable
#>

Get-help -Name Start-Process -Examples
# Results
<#
Start-Process -FilePath "sort.exe"
Start-Process -FilePath "myfile.txt" -WorkingDirectory "C:\PS-Test" -Verb Print
Start-Process -FilePath "Sort.exe" -RedirectStandardInput "Testsort.txt" -RedirectStandardOutput "Sorted.txt" -RedirectStandardError 
Start-Process -FilePath "notepad" -Wait -WindowStyle Maximized
Start-Process -FilePath "powershell" -Verb runAs
$startExe = New-Object System.Diagnostics.ProcessStartInfo -Args PowerShell.exe
$startExe.verbs
Start-Process -FilePath "powershell.exe" -Verb open
Start-Process -FilePath "powershell.exe" -Verb runas

#>
Get-help -Name Start-Process -Full
Get-help -Name Start-Process -Online


powershell /?
# Results
<#

PowerShell[.exe] [-PSConsoleFile <file> | -Version <version>]
    [-NoLogo] [-NoExit] [-Sta] [-Mta] [-NoProfile] [-NonInteractive]
    [-InputFormat {Text | XML}] [-OutputFormat {Text | XML}]
    [-WindowStyle <style>] [-EncodedCommand <Base64EncodedCommand>]
    [-ConfigurationName <string>]
    [-File <filePath> <args>] [-ExecutionPolicy <ExecutionPolicy>]
    [-Command { - | <script-block> [-args <arg-array>]
                  | <string> [<CommandParameters>] } ]

...

-Command
    Executes the specified commands (and any parameters) as though they were
    typed at the Windows PowerShell command prompt, and then exits, unless 
    NoExit is specified. The value of Command can be "-", a string. or a
    script block.

    If the value of Command is "-", the command text is read from standard
    input.

    If the value of Command is a script block, the script block must be enclosed
    in braces ({}). You can specify a script block only when running PowerShell.exe
    in Windows PowerShell. The results of the script block are returned to the
    parent shell as deserialized XML objects, not live objects.

    If the value of Command is a string, Command must be the last parameter
    in the command , because any characters typed after the command are 
    interpreted as the command arguments.

    To write a string that runs a Windows PowerShell command, use the format:
    "& {<command>}"
    where the quotation marks indicate a string and the invoke operator (&)
    causes the command to be executed.

...
#>

Start-Process (Microsoft.PowerShell.Management ... https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/start-process?view=powershell-7

Parameters

-ArgumentList Specifies parameters or parameter values to use when this cmdlet starts the process. Arguments can be accepted as a single string with the arguments separated by spaces, or as an array of strings separated by commas.

If parameters or parameter values contain a space, they need to be surrounded with escaped double-quotes. For more information, see about_Quoting_Rules.

Running command via a cmdlet, or powershell.exe are two different things. Each has its quoting specifics. You get these types of errors, because of incorrect syntax which includes needed quoting.

So, for your use case, Start-Process would be something like this:

$ConsoleCommand = "Copy-Item \\Zuul\IT\ffmpeg -Destination 'C:\Program Files\' -Force -Recurse"
Start-Process powershell -ArgumentList '-NoExit',"-Command  &{ $ConsoleCommand }" 

For PowerShell.exe, something like this:

PowerShell -Command {Copy-Item \\Zuul\IT\ffmpeg -Destination 'C:\Program Files\' -Force -Recurse}

Or this

PowerShell -Command "& {Copy-Item \\Zuul\IT\ffmpeg -Destination 'C:\Program Files\' -Force -Recurse}"

Thye can be combined, say if you are in the ISE/VScode, and you want to shell out a command to a new instance while staying in the ISE/VSCode, then, something like this:

Function Start-ConsoleCommand
{
    [CmdletBinding(SupportsShouldProcess)]

    [Alias('scc')]

    Param  
    ( 
        [string]$ConsoleCommand,
        [switch]$PoSHCore
    )

    If ($PoSHCore)
    {Start-Process pwsh -ArgumentList "-NoExit","-Command  &{ $ConsoleCommand }" -Wait}
    Else
    {Start-Process powershell -ArgumentList "-NoExit","-Command  &{ $ConsoleCommand }" -Wait}

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.