Windows PowerShell

Microsoft has developed a more robust command line interface (CLI) for Windows XP and Windows Vista called Windows PowerShell that is supposed to bring to Windows a Unix style command line. It can be downloaded and installed for free from this link:

JJM 2016-09-18 PowerShell is now open source PowerShell on GitHub .

JJM 2016-01-09 The link above does not appear to work anymore.

JJM 2016-01-09 Eagle allows bidirectional integration with PowerShell and combined with Garuda it can allow access from/to native Tcl.

snichols I tried it and it's much better then DOS, but comes with many brand new commands, so there's a learning curve involved. There are some commands that will look familiar ported from Unix, like diff and ls. You can do math operations right within the shell and call other programs like BASH or DOS does. Has anyone else tried and what did you think?

LV So, what's the connection to Tcl?

snichols They're both interpreted languages. Yet another choice for a programmer when they need to script something, but PowerShell's use would only be for Windows oriented tasks. Bash runs on multiple platforms like Linux, Unix, Mac, etc, and PowerShell is unique to Windows, but still much more robust then the predecessor DOS.

LV Microsoft purchased Interix who makes a POSIX Windows kernel that provides quite a useful command line environment that includes ksh, perl, tcl and other pieces. It was called Microsoft Windows Services for UNIX (SFU). See details at . Note that while it is available for free download for Windows XP, a number of its components come in Windows Server 2003 R2 and Vista. I don't know how one completes those environments.

MHo: It should be noted that the PowerShell CLI requires the .NET-Environment. IMHO this is a little overweight for a command line interface. And why is MS constantly busy in reinventing the wheel? There are already enough CLIs on this world, like BASH (oder TCLSH ;-), which are already documented, available nearly everywhere and well known. We all remember, a few years ago, MS claimed VBScript to be the definitve scripting environment. And so it goes on and on and on and on...

esfour That's because Windows broken by design all-is-binary concept is not compatible with the usual shell mechanisms. Well, the csh-like syntax of the Windows PowerShell is appalling, but at least one is able to retrieve data from a windows object and convert it to the users preferred format. I'm still not sure if WPS is the answer (and I cannot remember if I ever asked the question), but it's way better than VBS.

sdw All that being said... if I get some time I'd love to hack a Tcl extension to expose the functions inside of PowerShell to a Tcl script. My organization has swallowed the Kool-Aid and I'm maintaining exchange boxen. While I can get around pretty well in PowerShell, what I'd really love to do is tie it in to the mass of scripts that I've already put together for managing Unix. Not to mention a trained chimp interface for handing to HR types so they can create and remove users without having to play around in Active Directory themselves.

RS: Can someone compare this to the bash offered by Cygwin or mingw on Windows?

LV I've not used PowerShell. What I have used are two other things - the free UWIN by David Korn and MicroSoft's free POSIX stuff (which has ksh, bash, etc.).

MJ - After having used PS for a short while, I do like the possibilities it offers. One thing I noticed is that the passing of objects instead of text removes the need for a lot of grep, cut, etc. uses in the pipeline. For example:

 ps tclsh | kill

will kill all running tclsh processes, there is no need to parse (or modify with switches) the ps output.

Anon - An old article from the PowerShell Team mentioning how Tcl/Tk was a huge influence on them:

MHo 2012-06.28: IMHO the point is not the language itself but it's tight integration into the serveral windows api´s. The same situation as with VBScript: VBS is not one of the best Basic Language Implementation; many basic constructs are absent; but it's seamless integration into the system made it so powerfull. WPS is more a concept for administering computers, not a language in it's own right. Comparing it with it 30 years old predecessor or even with the by far more powerfull cmd.exe is like comparing apples with pears. Don't know if I'm right as I came from windows, but I think unix shells like bash, ksh etc. are much more powerfull then the traditional dos/windows shells, but less powerfull or different from design than powershell, as powershell opens the whole .NET api layer to the programer, if I understand it right.

LV Well, it's been years since this discussion occurred. Since that time, my job has mutated into one in which powershell and cygwin have become a part of my daily routine. One thing that I have not yet found is a module (aka extension) for powershell as elegant as Tk. To me, this seems like such a natural pairing. Powershell's ability to obtain most items from the environment as objects, and Tk's elegance in describing GUI. All I have found so far are examples of digging into .Net or use of Visual Studio to create interfaces.

I suspect that other options are available - I'm still digging.

MHo Here's a little helper to call powershell commands from your tcl script. I used code found on this wiki here and there...

##### Calls a single Powershell command (blocking, hidden)
### Arg: The command to give to Powershell via -command switch
### Ret: A List of three elements:
###  -1 "" <errtext>       -> error from packa re or create_process (twapi)
###   0 <stdouttxt> ""     -> Ok
###   1 "..." <stderrtext> -> Maybe Ok, something written to stderr
proc execPowershellCmd {cmd} {
     set cmd "-command $cmd"
     foreach chan {stdin stdout stderr} {
             lassign [chan pipe] rd$chan wr$chan
     if {[catch {
        package require twapi_process
        set cmd [string map [list \" \\\"] $cmd]
        twapi::create_process [auto_execok powershell] -cmdline $cmd -showwindow hidden \
         -inherithandles 1 -stdchannels [list $rdstdin $wrstdout $wrstderr]
     } ret]} {
        return [list -1 "" $ret]
     chan close $wrstdin; chan close $rdstdin; chan close $wrstdout; chan close $wrstderr
     foreach chan [list $rdstdout $rdstderr] {
             chan configure $chan -encoding cp850 -blocking true; # -buffering full?; # -enc?
     set out [read $rdstdout]; set err [read $rdstderr]
     chan close $rdstdout; chan close $rdstderr
     return [list [string compare $err ""] $out $err]

MHo Other possibilities:

# only with tclsh, not wish, because wish has no stdin
catch {exec -ignorestderr -- powershell -ExecutionPolicy Bypass -command - << {
write-host "Guten Abend von der PowerShell!"
} 2>@1} ret

or maybe this one:

catch {exec -ignorestderr -- powershell -ExecutionPolicy Bypass -noninteractive -command - << {
write-host "Guten Abend von der PowerShell!"
# testing an error
} 2>@1} ret
puts $ret

This one needs some helper code (not shown here):

source ooBgExec1.tcl

proc cb {obj typ {data ""}} {
     switch -nocase $typ {
        "eof" {
           puts "<EOF>"
        "timeout" {
        "data" {
           puts "$data"
        "nodata" {
        default {
           puts "<Fehler:> $data"

set cmd {& {write-host "Guten Abend von der PowerShell..."
start-sleep -s 10
write-host "...10s Wartezeit sind um!"

puts [bgExec new "powershell -ExecutionPolicy Bypass -command $cmd" cb]
puts [bgExec new "cmd.exe /c echo Dies ist ein Test via cmd.exe" cb]

while {$::bgExecVwaitVar > 0} {
    vwait ::bgExecVwaitVar

Or, for sporadic interactions, use such a construct:

proc GetData {chan} {
    if {[gets $chan line] >= 0} {
        puts $line
    if {[eof $chan]} {
        close $chan; # cancelt fileevent
        set ::eof 1

set h [open "|powershell -ExecutionPolicy Bypass -noninteractive 2>@1" r+]
fconfigure $h -blocking 0 -buffering line -translation crlf
fileevent $h readable [list GetData $h]

puts $h {write-host "Guten Abend von der PowerShell..."
start-sleep -s 5
write-host "...10s Wartezeit sind um!"

puts $h "3+7"
puts $h "exit"; # beendet diese PS-Sitzung

vwait ::eof

Another variation. Still havent found the definitive way to call powershell commands. In addition, I've no idea how to best treat the returned results and how to avoid blocking due to unresponded powershell prompts/waits....:

############### Problem: es dürfen keine Eingabeprompts kommen!
set errLvl 0; # Default - evtl. -1
set catched [catch {exec -ignorestderr -- powershell -ExecutionPolicy Bypass -noninteractive -command - << {
write-host "Guten Abend von der PowerShell..."
start-sleep -s 10
write-host "...10s Wartezeit sind um!"
} >&@stdout} ret options]
if {$catched && [dict exists $options -errorcode]} {
 set details [dict get $options -errorcode]
 if {[lindex $details 0] eq "CHILDSTATUS"} {
    set errLvl [lindex $details 2]
#puts "catched: $catched"
#puts "errLvl : $errLvl"
#puts "ret    : $ret"
flush stdout
exit $errLvl