Multiple PowerShell commands from BAT file.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
karanik
Posts: 1
Joined: 16 Dec 2020 03:55

Multiple PowerShell commands from BAT file.

#1 Post by karanik » 16 Dec 2020 04:50

Hello,

I want help to understand some things and to resolve some issues.
I am trying to run multiple PowerShell commands from a bat file. I have problem on output or with execution of some commands.
For Example

Question1
I have test.bat

Code: Select all

@echo off
echo. > .\user-info.txt 
whoami >> .\user-info.txt  
echo. >> .\user-info.txt  
wmic /APPEND: ".\user-info.txt " USERACCOUNT get Caption,Name,PasswordRequired,Status
net user >> .\user-info.txt  
echo. >> .\user-info.txt  
::
echo === DISK INFORMATION === >> .\user-info.txt  
PowerShell.exe -nologo -noprofile -ExecutionPolicy Bypass -Command "& {get-psdrive -psprovider filesystem  }" >> .\user-info.txt
wmic /APPEND: ".\user-info.txt " diskdrive get Name,InterfaceType,MediaType,Model,Size,Status
echo. >> .\user-info.txt  
::
echo === Top 10 CPU Processes === >> .\user-info.txt  
echo ----------------------------------- >> .\user-info.txt  
PowerShell.exe -nologo -noprofile -ExecutionPolicy Bypass -Command "& {Get-Process | Sort-Object -Property CPU | Select-Object -Last 10 }" >> .\user-info.txt
echo. >> .\user-info.txt 
::
echo === Chkdsk Logs === >> .\user-info.txt  
echo ----------------------------------- >> .\user-info.txt  
PowerShell.exe -nologo -noprofile -ExecutionPolicy Bypass -Command "& {get-winevent -FilterHashTable @{logname="Application"; id="1001"}| ?{$_.providername -match "wininit"} | fl timecreated, message }" >> .\user-info.txt
echo. >> .\user-info.txt  
The output give that file and i have one error on last PowerShell command >> PowerShell.exe -nologo -noprofile -ExecutionPolicy Bypass -Command "& {get-winevent -FilterHashTable @{logname="Application"; id="1001"}| ?{$_.providername -match "wininit"} | fl timecreated, message }" >> .\user-info.txt
I don't know why. Probably from character { and } but i don't know alternative method to bypass this.
image1 -https://imgur.com/f3s9GlP
Image


Question2
If i change the output method from this

Code: Select all

>> .\user-info.txt
to this

Code: Select all

Out-File '.\user-info.txt' -Append

then the results is like this on Notepad++ [image2] , and this if because has many spaces charecters [image3].
image2 - https://imgur.com/heKUCDH
Image

image3 - https://imgur.com/ebWxlFD
Image

aGerman
Expert
Posts: 4043
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Multiple PowerShell commands from BAT file.

#2 Post by aGerman » 16 Dec 2020 10:25

Things that you could try:
- The entire command is enclosed into quotes. So it's likely that you must escape quotes inside of the powershell command. Try \" or `". Alternatively use single quotes ' instead of double quotes " for the strings.
- Obviously the Out-File cmdlet appends UTF-16-encoded text. Changing the encoding may help

Code: Select all

Out-File -Encoding ([Text.Encoding]::GetEncoding(1252)) -Append '.\user-info.txt'
Steffen

Post Reply