Run Powershell script from inside other Powershell script with dynamic redirection to file

I have been struggling with this problem and researching around but can't get a solution

My problem: I need to run a Powershell script from inside another Powershell script and redirect the output stream to a file. So far so good. The real issue comes when I need to control the amount of logging through a variable (e.g. write only errors or only error + warning + success output streams).

I can get around it by hard-coding the command as in:

Powershell -File "\path\myscript.ps1" 2>&1> $logFilePathAndName

However, I want to give the user a couple of options for the redirect operator and want to avoid hardcoding each one of them. For that, I was thinking to just code something similar to :

$logStreams = "2>&1>" Powershell -File "\path\myscript.ps1" $logStreams $logFilePathAndName

The last command does run my script (myscript.ps1 has no input params) but it does not write anything to the file at $logFilePathAndName.

I tried various syntax with Invoke-Command, Invoke-Expression, the call operator and Powershell -Command with no luck.

Looked at this post, had several tries but I can't just get it to work. For example this runs my script but does not write anything to the output log:

$logStreams = "2>&1>" $command = '"C:\myscript.ps1" $_logStreams "C:\outputlog.txt"' iex "& $command"

Is there a way to pass a variable string for the redirect operator OR run a string containing the entire command with a variable interpolated for the output redirector ?


Your last bit there is very close, but I'm not sure why you have the underscore in there, and you need to escape your dollar sign, and close the entire thing in double quotes to cause string extrapolation.

$logStreams = "2>&1>"
$command = "'C:\myscript.ps1' $logStreams 'C:\outputlog.txt'"
iex "& $command"

I just tested that locally and it works fine.


  • Using VSTS Rest API, how do you Update a Markdown widget?
  • How to make python script executable on osx?
  • Powershell 2: Easy way to direct every bit of output to a file?
  • Error getting audio input device sample rate: '!obj'
  • PowerShell execution policy subverted by powershell.exe parameter?
  • PHP - Kill proc_open() process on different file?
  • When try to execute a cgi script, get the error: “[WinError 193] %1 is not a valid Win32 application
  • powershell: script to start a program with parameters?
  • coldfusion variables into powershell
  • integration of function in modelica
  • Basic defensive programming [duplicate]
  • A class implementing two different IObservables?
  • TFS - how do I sum child task hours to parent
  • C++ Single function pointer for all template instances
  • How to access meteor package name inside package?
  • opencv display image without x server
  • How to make R's read_csv2() recognise the text characters properly
  • How to get latest version of a artifact on Bintray using JSONP
  • Tell Git to stop prompting me for conflicts when none really exist?
  • IE11 throwing “SCRIPT1014: invalid character” where all other browsers work
  • nonblocking BIO_do_connect blocked when there is no internet connected
  • How solve “Qt: Untested Windows version 10.0 detected!”
  • C: Incompatible pointer type initializing
  • Make VS2015 use angular-cli ng at build time in a .NET project
  • Checking free space on FTP server
  • Get data from AJAX - How to
  • Jenkins: How To Build multiple projects from a TFS repository?
  • How do I fake an specific browser client when using Java's Net library?
  • Bug in WPF DataGrid
  • script to move all files from one location to another location
  • ILMerge & Keep Assembly Name
  • Symfony2: How to get request parameter
  • How do you troubleshoot character encoding problems?
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • Why winpcap requires both .lib and .dll to run?
  • Apache 2.4 - remove | delete | uninstall
  • WOWZA + RTMP + HTML5 Playback?
  • Load html files in TinyMce
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • coudnt use logback because of log4j