Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Invisible Windows

389 views
Skip to first unread message

Jon

unread,
Aug 12, 2007, 3:20:46 PM8/12/07
to
Having developed an aversion to black boxes appearing on the screen over the
years, what I think would be a really cool feature for this powershell would
be the ability to run PowerShell scripts invisibly.

Now, of course, I can wrap a .ps1 script in say something like a Windows
Script Host script like say (VBScript).....


Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "MyPowerShellScript.ps1",0


and with the correct .ps1 file association set up it will run invisibly, but
it would be nice to be able to do this with the need for any wrapper at all.

ie something along the lines of

powershell -Invisible <Path to script>

I don't know if the powershell team follow this newsgroup, but that would be
my powershell suggestion of the day, or perhaps the functionality's there
already, and I've just missed it?

--
Jon


Edengundam

unread,
Aug 12, 2007, 9:49:40 PM8/12/07
to
Redirectting the stdout to Out-Null or $null maybe can accomplish what u
want :)

just like:
powershell "& 'c:\......\test.ps1'" > $null
powershell "& 'c:\......\test.ps1'" | Out-Null

best wishes

"Jon" <Email_...@SomewhereOrOther.com> wrote:
news:eUf3iXR...@TK2MSFTNGP06.phx.gbl...

Jon

unread,
Aug 13, 2007, 2:08:14 AM8/13/07
to
Hi. Thanks for the reply. Yep, similar to that solution, except that the
'black box' would never appear at any stage of the process

ie if I ran a script such as this one (by double-clicking on a .ps1 file),
then all that would be seen would be the MessageBox and no black box....


[Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Windows.Forms.MessageBox]::Show("Hello")


I suspect that I'll create a simple replacement .exe, whose sole purpose
will be to launch the powershell.exe and the script invisibly, and associate
.ps1 files with that instead. So all is not lost :-) , but it would be
simpler if it were just an extra switch in launching powershell.exe, in
which case I could add it to the registry association for the .ps1
extension.

--
Jon

"Edengundam" <feng...@163.com> wrote in message
news:%23AF%234vU3H...@TK2MSFTNGP03.phx.gbl...

Gerd Schneider

unread,
Aug 15, 2007, 4:28:01 AM8/15/07
to
Hi,

I'm monitoring this DL for a while and really wonder why you are the first
in months to explicitly asking for that – I also miss this functionality.

Since PS is a hostable scripting environment we basically would need a
Windows-based host for PS, analogous to wscript.exe for VBScript.
powershell.exe, as we get it today from MS, is a Console-based host, similar
to cscript.exe for VBScript, so any attempt to get rid of the black box using
some kind of hiding wrapper is just a clumsy workaround but not a clean and
straightforward solution.

Basically any talented and experienced C# programmer (unfortunately that’s
not me) could do that for the PS community since the required interfaces are
available and well documented, but maybe the demand is currently not
expressed as clear as it should be.

Gerd

Jon

unread,
Aug 15, 2007, 6:20:23 AM8/15/07
to
Thanks for the reply Gerd. Agree with you completely.

I'm currently using the 'permanent wrapper' style solution, in that I've
re-associated .ps1 files with an executable that relaunches powershell.exe
invisibly, with the arguments passed to it . I also use a toggle switch
between the 2 ways of running (similar to toggling between the 2 hosts
cscript.exe and wscript.exe).

This works fine, although I had a few hiccups initially with forms not
appearing, but with a few minor modifications to the code (using the 'Run'
method of [System.Windows.Forms.Application] (there may be better ways)) I
managed to resolve these.

I'm not really up to writing a completely new substitute host for powershell
either, which I'm sure would involve a lot of hard work and headaches, but
hats off to anyone who does.

It would be nicer imho if this functionality were implemented from the top
down, in an analogous way to the WScript.exe / CScript.exe model, which
would enable 'black boxless' powershell scripts to be distributed far more
easily, and in turn I'm sure would also contribute to a massive explosion in
the popularity of powershell.

--
Jon


"Gerd Schneider" <GerdSc...@discussions.microsoft.com> wrote in message
news:3C9B0596-D7C4-4205...@microsoft.com...

Bruce Payette [MSFT]

unread,
Aug 15, 2007, 2:43:09 PM8/15/07
to
We do monitor the newsgroup and yes, this is on the feature list for v2,
probably as a -hide flag on PowerShell.exe. It might also be something that
you can convince the PSCX folks to add to their package since they release
more often we do. Keith?

In the meantime, here is a script that will let you hide the console window.
Do
set-console -hidden
to hide the console and
set-console
to make it visible again. Typical test sequence is:

set-console -hidden ; start-sleep 5 ; set-console

Extract the script from this message and save it as Set-Console.ps1 in your
path.

-bruce

----------------------------cut here--------------------
#
# Sample script showing how to show and hide
# console windows from PowerShell. It uses in-line
# CSharp code to create a ConsoleHelper class to show
# and hide the console.
#

param ([switch] $hidden)

#
# CSharp code for the ConsoleHelper class
#
$code = @'
using System;
using System.Runtime.InteropServices;
public class ConsoleHelper
{
private const Int32 SW_HIDE = 0;
private const Int32 SW_SHOW = 5;

[DllImport("user32.dll")]
private static extern Boolean ShowWindow(
IntPtr hWnd,
Int32 nCmdShow);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool AllocConsole();

[DllImport("Kernel32.dll")]
private static extern IntPtr GetConsoleWindow();

public static void HideConsole()
{
IntPtr hwnd = GetConsoleWindow();
if (hwnd != IntPtr.Zero)
{
ShowWindow(hwnd, SW_HIDE);
}
}

public static void ShowConsole()
{
IntPtr hwnd = GetConsoleWindow();
if (hwnd != IntPtr.Zero)
{
ShowWindow(hwnd, SW_SHOW);
}
}
}
'@

#
# Only build the assembly if the type doesn't exist
#
# if this statement fails, then the trap handler will
# compile the code
[ConsoleHelper] > $null
$ch = [ConsoleHelper]
trap {
# Get an instance of the CSharp code provider
$cp = new-object Microsoft.CSharp.CSharpCodeProvider
# And compiler parameters...
$cpar = New-Object System.CodeDom.Compiler.CompilerParameters
$cpar.GenerateInMemory = $true
$cpar.GenerateExecutable = $false
$cpar.OutputAssembly = "custom"
$cr = $cp.CompileAssemblyFromSource($cpar, $code)
# display any errors (there should be none...)
if ( $cr.Errors.Count)
{
$codeLines = $code.Split("`n");
foreach ($ce in $cr.Errors)
{
write-host "Error: $($codeLines[$($ce.Line - 1)])"
$ce | out-default
}
Throw "Compile failed..."
}
else
{
# don't report the exception
continue
}
}

#
# Now use the helper to show and hide the window...
#
if ($hidden)
{
$ch::HideConsole()
}
else
{
$ch::ShowConsole()
}
----------------------------cut here---------------------

--
Bruce Payette [MSFT]
Windows PowerShell Technical Lead
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.

"Jon" <Email_...@SomewhereOrOther.com> wrote in message
news:eUf3iXR...@TK2MSFTNGP06.phx.gbl...

Jon

unread,
Aug 15, 2007, 3:51:46 PM8/15/07
to
Thanks for the reply Bruce. Encouraging to know you guys read actually read
this stuff ;-)

Thanks also for the script. Works great. A number of interesting techniques
in there to learn from - will be exploring that in detail.

Also looking forward to version 2. That's great news

Actually I'm excited about powershell which I may not have conveyed in my
original post, and with a few minor modifications like this, even more so.
It has a massive potential imho, and is quite likely to make technologies
such as VBScript / CScript (which I know and love) to ultimately seem like
childs' play by comparison.

So keep up the good work! Cheers.

--
Jon


"Bruce Payette [MSFT]" <bruc...@microsoft.com> wrote in message
news:uqlQhw23...@TK2MSFTNGP05.phx.gbl...

Keith Hill [MVP]

unread,
Aug 16, 2007, 2:01:14 PM8/16/07
to
"Bruce Payette [MSFT]" <bruc...@microsoft.com> wrote in message
news:uqlQhw23...@TK2MSFTNGP05.phx.gbl...
> We do monitor the newsgroup and yes, this is on the feature list for v2,
> probably as a -hide flag on PowerShell.exe. It might also be something
> that you can convince the PSCX folks to add to their package since they
> release more often we do. Keith?

It's already there: :-)

start-process powershell.exe -Arguments '-noprofile
C:\users\hillr\foo.ps1' -NoWindow -NoShellExecute

--
Keith
http://www.codeplex.com/powershellcx

0 new messages