How to execute .cmd file within powershell script? - powershell

I have cmd file, look like this
set Name=Joy
I want to execute that cmd file within my powershell script, then I want to see the value of cmd file from command line with command echo %Name% and It should show Joy.
But I can not figure it out, I tried this way, but its not working,
1st Way
$File = Get-Command .\TEST.cmd
& .\TEST2.cmd
2nd Way
Invoke-Expression ".\TEST.cmd"
3rd Way
&.\TEST.cmd

Related

How to call batch from powershell so I can see realtime output in powershell console

I need to call batch from powershell with specific working directory. I don't want new console to be opened, but I would like to see the output of the batch as a part of the output from powershell script.
I could forward the standard output to file and then write it by Write-Host, but the batch takes time and I would like to see the output in realtime as it is processing.
I tried Process-Start, but I don't know how to redirect standard output of batch to standard output of powershell.
You can add 'cmd /c ' to your ps script, and to change directories, you can use #jisaak suggestion, so:
$wd = Get-Location;
Set-Location "batch file directory";
cmd /c "Your batch";
Set-Location $wd;
That will open a new cmd in the current console and all the output will be directed to it(when the batch will reach EOF the cmd terminates).

Batch File - Commands not executing after Powershell command

I have written simple batch script to replace some text in a file, when executed via command line but when those commands are copied to .bat file execution stops after powershell command. Any idea how to execute powershell commands in batch file?
arco444's answer worked for me in comments:
You just call powershell.exe with no parameters so it starts an interactive session. If you were to type exit your batch script would continue. Use the -command switch to execute whatever it is you want to. And please edit your question and include the code there. Also You need to put the powershell command in quotes. i.e. powershell -command "get-content file.cs"

I want to write a powershell script which will open cmd and execute the desired command

basically i want to open any window application and want to input any command through script.
I want to write a powershell script which will open cmd and execute the desired command.
i am able to start cmd using powershell but not able to pass command.
eg i want to open cmd using powershell and execute copy command on cmd .
If you want to copy the file specifically in cmd you can use cmd with switch /c to execute the command you want:
cmd /c copy C:\Temp\abcd.txt C:\Temp\abcd2.txt
On the other hand you can easily achieve the same functionality using Powershell's Copy-Item command:
Copy-Item -Path C:\Temp\abcd.txt -Destination C:\Temp\abcd2.txt

VB equivalent to powershell

I am very new to powershell and sometimes this question may be so simple
Can please anybody please tell me what is the equivalent to the following(vbscript) in PowerShell
set obj = wscript.createobject( wscript.shell )
Obj.Run $smCmnd
What is the use of wscript.shell.
After a bit of search I found first line can be presented as;
$obj = New-Object -ComObject WScript.Shell
But have have no idea how to call Run method ($obj.run(...)???)
If I run cmd.exe with some commands as the smCmnd, How can I keep cmd.exe without close and to run another command later in same console??
EDIT
I am writing PS script and it will be call from another application. Basically it will do some folder creations and file coping etc. I would like to open CMD.exe and show all the commands running on that. How to use same cmd prompt through out my whole script.
Is smCmnd a string of shell commands? If so, you can call them directly from PowerShell, without trying to get a wscript.shell COM object to run them against like you'd need to do in VBScript.
VBScript wasn't a shell. Powershell is. You can write shell commands directly in .ps1 or .ps2 files, just like in a batch file.
I'm not a powershell expert here, but try doing
& $smCmnd
Try running $smCmnd directly. If that fails, use Invoke-Expression $smCmnd.
If you do need to use CMD.EXE (possibly because you want to run pre-existing BAT file), and you want all of the output in a single CMD window you can pipe all the input into cmd at once like this:
# Powershell script to execute existing BAT file
cmd.exe /k "cd c:\batchfiles & firstone.bat & second.bat & echo that's all folks"
# CMD will remain open (/k). User will have to type exit to return to powershell
# Or if you want user just to hit any key to leave CMD prompt:
cmd.exe /c "c:\batchfiles\mybatchfile.bat & pause"
# /C means CMD should close after is has executed the commands on the command line
However if you want to have something execute in CMD, then make a decision in your Powershell script about what to execute next in CMD then do something similar to the answer at the following link which pipes input and output between a powershell script and CMD.exe.
How to run interactive commands in another application window from powershell

How can I detect whether or not I am in powershell from a command line?

I am creating a standard windows BAT/CMD file and I want to make an IF statement to check whether or not this CMD file is run from PowerShell. How can I do that?
Edit: My underlying problem was that test.cmd "A=B" results in %1==A=B when run from CMD but %1==A and %2==B when run from PowerShell. The script itself is actually run as an old Windows Command line script in both cases, so checking for Get-ChildItem will always yield an error.
One way, it to see what your process name is, and then check its attributes:
title=test
tasklist /v /fo csv | findstr /i "test"
As long as you use a unique name in place of Test, there should be little room for error.
You will get back something like:
"cmd.exe","15144","Console","1","3,284
K","Running","GNCID6101\Athomsfere","0:00:00","test"
When I ran the above code from a bat file, my output was:
"powershell.exe","7396","Console","1","50,972
K","Running","GNCID6101\Athomsfere","0:00:00","
A potentially simpler approach that may work for you. If not, it may be suitable for others.
Create 2 separate script files: test.ps1 and test.cmd
Don't include extension when calling the script. I.e. call as <path>\test (or just test if folder is in the path environment variable.
This works because CMD prioritises which script to execute as: .bat > .cmd, whereas Powershell prioritises: .ps1 > .bat > .cmd.
The following is the output of a CMD session:
C:\Temp>copy con test.cmd
#echo cmd^Z
1 file(s) copied.
C:\Temp>copy con test.ps1
Write-Output "ps1"^Z
1 file(s) copied.
C:\Temp>.\test
cmd
C:\Temp>
And calling test from Powershell:
PS C:\Temp> .\test
ps1
PS C:\Temp>
Couldn't you try to execute a Get-ChildItem and then check %ERRORLEVEL% to see if it returns an exe not found?
http://ss64.com/nt/errorlevel.html