Run PowerShell code from batch file

July 31, 2018 at 23:55:34
Specs: Windows 10
Can any one help me on below:

How can I run the below Powershell code in bacth file.

Get-WmiObject -Class win32_product | Where-Object {($_.Name -eq "MSystem" -and $_.version -eq "1.1")} | select __Server , name , version|`
Out-File c:\temp\Mess.txt
Invoke-Expression c:\temp\Mess.txt


So far I tried below and saved this as .bat file , but cannot see results

---------------

@echo off

PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Get-WmiObject -Class win32_product | select __Server, Name , version | Where-Object {($_.Name -eq "MSystem" -and $_.version -eq "1.1"))} | Out-File c:\temp\MS_Info.txt};
Invoke-Expression c:\temp\Mess.txt ";


See More: Run PowerShell code from batch file

Reply ↓  Report •

#1
August 1, 2018 at 01:11:09
try something like:
cmd /K "PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Get-WmiObject -Class win32_product | select __Server, Name , version | Where-Object {($_.Name -eq "MSystem" -and $_.version -eq "1.1"))} | Out-File c:\temp\MS_Info.txt};
Invoke-Expression c:\temp\Mess.txt "; "

it has to be a one-liner tho and you may have to escape characters like |
You could also just make the .bat file call a .ps1 file:

call "C:\filelocation\powershell_file.ps1"

i5-6600K[delid]@4.8GHz/4.3GHz@1.4v LLC=6 | 2x4GB Crucial-DDR4-2133CL15@14-14-14-30 1T 2700MHz@1.37v
MSI Armor RX 570 4GB@1395Mhz core@1.168v/1920MHz BiosModded

message edited by hidde663


Reply ↓  Report •

#2
August 1, 2018 at 07:28:12
- You're saving the output to "c:\temp\MS_Info.txt", but then try to open "c:\temp\Mess.txt".
- If Mess.txt/MS_Info.txt is being saved, but it's empty, then nothing's making it past your Where-Object.
- Your ending curly bracket needs to be just before the ending double quote.
- You have double quotes in a double-quoted string, which is not a good idea.

I generally find it easier to get my commands working in PowerShell, convert those commands into Base64, and use -EncodedCommand instead of -Command.

How To Ask Questions The Smart Way


Reply ↓  Report •
Related Solutions


Ask Question