locked
Starting PowerShell with Start-Process RRS feed

  • Question

  • Hi,

    I'm trying to start PowerShell using Start-Process. Task looks simple for sure.

    Start-Process -FilePath C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ArgumentList "-NoExit","-command `"& { get-date } "

    I can handle variables also,

    Start-Process -FilePath C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ArgumentList "-NoExit","-command `"& { `$date = get-date; `$date } "

    Main problem is that I would like to have "here" strings inside script block.

    $str = @"

    text

    "@

    But I was not able to find how to do this.

    Any help is appreciated.

    Krunoslav

    Tuesday, August 1, 2017 5:06 PM

All replies

  • Why?  Just use a file:

    Start-Process powershell.exe -ArgumentList '-NoExit -File .\myscript.s1'


    \_(ツ)_/

    Tuesday, August 1, 2017 5:25 PM
  • In my case file is not a option, but I got hint (thanks to Aleksandar Nikolic) to use encrypted command and it worked like charm.

    $command = @'

    anything

    '@

    $bytes = [System.Text.Encoding]::Unicode.GetBytes($command)
    $encodedCommand = [Convert]::ToBase64String($bytes)
    $arg = "-NoExit", "-encodedCommand $encodedCommand"


    Start-Process -FilePath C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -ArgumentList $arg


    Tuesday, August 1, 2017 5:56 PM
  • Remember that the command line has a length limitation.

    \_(ツ)_/

    Tuesday, August 1, 2017 5:59 PM
  • Hi,
    Just checking in to see if the information provided was helpful. Please let us know if you would like further assistance.

    Best Regards,

    Frank

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Wednesday, September 6, 2017 7:14 AM