2

I am trying to have powershell spit out the output file from a command to the current folder as I am trying to create a printer migration that can be run from a USB stick.

My command is simply: C:\Windows\System32\spool\tools\Printbrm.exe /b /f C:\Temp\Print

but I want the file to save to the directory that the script is being run from which can very between D: and and E: because the USB path changes on different computers in our organization.

Anyone able to help with this? I am guessing it is simple but can't figure it out.

1
  • 2
    $PSScriptRoot, returns the path the script is saved at/ran from. Commented Mar 9, 2021 at 20:07

1 Answer 1

1

You can get the path containing the executing script from $MyInvocation:

$scriptFolder = $MyInvocation.MyCommand.Path | Split-Path -Parent

If you want the folder from which you're invoked it (since the title says "current directory"):

$currentFolder = (Get-Location).Path

To run your command and have it dump the output where the script is running:

$scriptFolder = $MyInvocation.MyCommand.Path | Split-Path -Parent
$printArgs = @("/b", "/f", $scriptFolder)
& "C:\Windows\System32\spool\tools\Printbrm.exe" $printArgs
Sign up to request clarification or add additional context in comments.

3 Comments

How would I implement that then? Adding that after the /f on the script doesn't work.
Added example - use Invoke-Expression to "run a string".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.