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:

http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx

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 http://en.wikipedia.org/wiki/Microsoft_Windows_Services_for_UNIX . 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: http://blogs.msdn.com/b/powershell/archive/2008/05/25/powershell-and-wpf-wtf.aspx


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 command.com 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]
}