6

I have a batch file that starts a PowerShell script.

Batch file:

START Powershell -executionpolicy RemoteSigned -noexit -file "MyScript.ps1"

MyScript.ps1 :

Write-Output "Hello World!"

It works fine, with one exception. The appearance of the window is like the old cmd.exe (black background) and not the PowerShell (blue background).
How can I get the true PowerShell window, if I start it from a batch file?

Thanks.

2
  • 2
    Thanks all who have posted so far. I don't really care if the background is blue or black. I want to know why this is happening in the first place. If I do this: START notepad then the Notepad starts normally and there is no trace of cmd.exe around. Notepad looks exactly the same as if I started it from a shortcut. Why is it different with PowerShell? Why is it different if I start it from a batch file vs from a shortcut? And I kinda like being able to paste with a right-click in the new PowerShell. Thanks. Commented Feb 18, 2012 at 2:47
  • Also, by default, Command Prompt only retains 300 lines whereas PowerShell retains MANY more lines so if you're using something like start powershell -noexit -executionpolicy unrestricted -file "PowerShell script.ps1" then you need it to actually open in PowerShell, not Command Prompt, so you can see the full history of the script. Commented May 25, 2016 at 14:42

3 Answers 3

6

If you really want a blue background, in your script add code to change the background color.

#save the original
$original=$host.ui.RawUI.BackgroundColor
$front=$host.ui.RawUI.ForegroundColor
$host.ui.RawUI.BackgroundColor="DarkBlue"
$host.ui.RawUI.ForegroundColor="White"
cls
#run your code
dir c:\scripts

#set it back
$host.ui.RawUI.BackgroundColor=$original
$host.ui.RawUI.ForegroundColor=$front
Sign up to request clarification or add additional context in comments.

Comments

3

That's a property of the shell link in the Start Menu which starts PowerShell, so you'd have to go through that:

start "" "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Accessories\Windows PowerShell\Windows PowerShell.lnk" ...

It's not pretty, it depends a little on where that resides (and might break on foreign language versions).

2 Comments

I tried it out on XP and no worky. I got a black shell. start "" "C:\Documents and Settings\All Users\Start Menu\Programs\Accessories\Windows PowerShell\Windows PowerShell.lnk". I had to use explorer.exe for it to honor the LNK properties.
Well, I'm on Windows 7 and it worked here. But that's what I was trying to tell you – it's a messy affair and likely to break down (especially in legacy OSes).
1

You can invoke powershell so that it starts itself with the script

Powershell.exe -Command "& {Start-Process PowerShell.exe -ArgumentList '-ExecutionPolicy RemoteSigned -noexit -File ""Full_Path_of_MyScript.ps1""'}"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.