This question already has answers here:
PowerShell - Start-Process and Cmdline Switches
(5 answers)
Closed 2 years ago.
How to let PowerShell use file as input to an executable?
for example, a directory like this
test.txt
text.exe
how to use test.txt as the input of test.exe in PowerShell?
Something like this should work
$settings =get-content "Path\test.txt"
$filename = "Path\text.exe"
Start-Process -FilePath $filename -ArgumentList $settings
Related
This question already has answers here:
Question about using PowerShell Select-String, exiftool(-k)
(2 answers)
Closed 2 years ago.
Power shell script to run a batch file:
start-Process C:\Test.bat |
Out-File C:\test.txt
when I check the text.txt file is empty?
Don't use Start-Process if all you need is to run a program in the more or less default way.
C:\Test.bat | Out-file C:\Test.txt
should work fine, although you most likely lack write privileges on C:\ so that's perhaps not the most useful path to use.
Start-Process does not redirect standard output by default. You have to tell it to do so:
Start-Process -FilePath .\test.bat -NoNewWindow -Wait -RedirectStandardOutput ".\test.txt"
Note, that this will not capture errors. If you want them too, add -RedirectStandardError ".\test-stderr.txt". This must be a different file than the one that you used for -RedirectStandardOutput.
This question already has answers here:
Why is no string output with 'echo %var%' after using 'set var = text' command in cmd? [duplicate]
(2 answers)
Closed 3 years ago.
I have powershell script which I am trying to execute using a batch file. I am not sure how to direct the .bat file to the powershell script without hardcoding the full path.
Both the script and .bat files are stored in the same folder. I've got this so far but it doesn't appear to do anything.
#ECHO OFF
REM PowerShell -WindowStyle Hidden -NoProfile....
SET ScriptDirectory = %~dpn0
SET ScriptPath = %ScriptDirectory% || "/" || counterscript.ps1
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& '%ScriptPath%'";
I don't think you can. You can only hard code the path. Maybe there is some way that you can do it, but I don't know any other ways rather than hard coding. You could hard code the file name though.
This question already has answers here:
Expand variable inside single quotes
(2 answers)
Closed 5 years ago.
I was using start-process cmdlet in powershell as
Start-Process -FilePath "$Installer_Path" -Args '/s /v"/qb SETUPTYPE=\"$Setup_type\" USERNAME=$user_name PASSWORD=$password SERVER=$sql_server INSTALLDIR=\"$Transfer_Path\\\" SHAREPATH=\"$Transfer_Path\\\""' –Wait
In the above command the variable are not getting replaced except $Installer_Path. I believe issue is because of variables located inside ' " " '. Could any one help me with the variable substitution?
Thanks.
The problem is that you are using single quotes and not double quotes. In order for your variables to expand you need to use double quotes. If you have to use double quotes inside of those you can escape them by preceding them with a backtick ` or by doubling them up "".
Start-Process -FilePath "$Installer_Path" -Args "/s /v`"/qb SETUPTYPE=\`"$Setup_type\`" USERNAME=$user_name PASSWORD=$password SERVER=$sql_server INSTALLDIR=\`"$Transfer_Path\\\`" SHAREPATH=\`"$Transfer_Path\\\`"`"" –Wait
This question already has answers here:
How can I output Handbrake output to both the screen and to a file?
(2 answers)
Closed 7 years ago.
When running a powershell script from the powershell console, I can redirect the output to a file like this:
powershell script.ps1 > file.txt
However, I need to redirect the output to a file AND still print it to the screen. How can I do this?
Use a transcript. You can do it one of two ways, the easiest is probably including these lines in your script:
In "Script.ps1"
Start-Transcript "C:\file.txt"
# the rest of your script
Stop-Transcript
The second way would be to call your script like so:
Start-Transcript "C:\file.txt"
.\Script.ps1
Stop-Transcript
Keep in mind that if you try to start a transcript, without the previous transcript not stopped, it will throw an error. So in your script, prior to the Start-Transcript command, maybe do this just to ensure the transcript isn't still running:
try{
Stop-Transcript
}catch{}
Start-Transcript "C:\file.txt"
# the rest of your script
Stop-Transcript
This question already has answers here:
What's the best way to determine the location of the current PowerShell script?
(15 answers)
Closed 9 years ago.
I run a PowerShell script. How do I get the directory path of this script I run?
How to do this?
PowerShell 3 has the $PSScriptRoot automatic variable:
Contains the directory from which a script is being run.
In Windows PowerShell 2.0, this variable is valid only in script modules (.psm1). Beginning in Windows PowerShell 3.0, it is valid in all scripts.
Don't be fooled by the poor wording. PSScriptRoot is the directory of the current file.
In PowerShell 2, you can calculate the value of $PSScriptRoot yourself:
# PowerShell v2
$PSScriptRoot = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition