![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | powershell and cmd Although I am enthusiastic about Powershell, I don't get many opportunities to use it at work. So, to try to immerse myself in Powershell, I have been trying to use it as a replacement for cmd. Questions: Is Powershell intended to be used as a true shell as well as a script environment, or should I view it more as a scripting environment that happens to be interactive? Is there a good resource for learning the "simple stuff"? Example: while I can find all kinds of great scripting examples in Powershell, what's the best way to do this in powershell: "dir /ad /on"? (note: I know that I can pipe output to sort for the /on part, but is that the expected/best way?) (note: I haven't gotten the /ad part to work yet, but I was trying to pipe the get-childitem output to Where, and perform some selection on the Mode, is that the best way to do it?) |
My System Specs![]() |
| | #2 (permalink) |
| | Re: powershell and cmd > Questions: > Is Powershell intended to be used as a true shell as well as a script > environment, or should I view it more as a scripting environment that > happens to be interactive? > yes. and its compatible with calling old cmd.exe stuff. i.e you call the native ipconfig command. powershell is MORE a shell than a scripting language. well it is both, but it designs leans towards the interactive scenario |
My System Specs![]() |
| | #3 (permalink) |
| | Re: powershell and cmd Thanks for your reply, but I still am not sure how to perform the operations that I stated in my original question. Are you suggesting that in order to do a "dir" command with filtering, I would need to invoke cmd.exe? Powershell seems like a great scripting environment, but if it can't even do a filtered "dir" without shelling to the old cmd processor, then I can't really classify it as a cmd replacement. <klumsy@xtra.co.nz> wrote in message news:1160426395.112255.141010@m73g2000cwd.googlegroups.com... > >> Questions: >> Is Powershell intended to be used as a true shell as well as a script >> environment, or should I view it more as a scripting environment that >> happens to be interactive? >> > > yes. and its compatible with calling old cmd.exe stuff. i.e you call > the native ipconfig command. powershell is MORE a shell than a > scripting language. well it is both, but it designs leans towards the > interactive scenario > |
My System Specs![]() |
| | #4 (permalink) |
| | Re: powershell and cmd Analogue of cmd "dir /ad /on" in PowerShell is: Get-ChildItem | Where-Object {$_.PsIsContainer} | Sort-Object Name or (ditto) dir | ? {$_.PsIsContainer} | sort Name -- Thanks, Roman |
My System Specs![]() |
| | #5 (permalink) |
| | Re: powershell and cmd "J.Marsch" <jmarsch@newsgroup.nospam> wrote in message news:ekmZj396GHA.3644@TK2MSFTNGP03.phx.gbl... > Although I am enthusiastic about Powershell, I don't get many > opportunities to use it at work. So, to try to immerse myself in > Powershell, I have been trying to use it as a replacement for cmd. > Questions: > Is Powershell intended to be used as a true shell as well as a script > environment, or should I view it more as a scripting environment that > happens to be interactive? As Klumsy says. This is a "scriptable shell". > Is there a good resource for learning the "simple stuff"? If the reference documentation doesn't _immediately_ tell you what you want to know, the very best thing you can do is post your questions here. This lets us know what's not very simple or intuitive. It also serves as a reality check for those of us who are off into reflection and dynamic code generation and PS-based Runge-Kutta analysis. ![]() That said, a good starting point is the documents in the $PSHome directory. + The PowerShell Primer %Systemroot%\System32\windowspowershell\v1.0\userguide.rtf This is a basic talking walk-through of simple aspects of PS, with quite a bit of demonstration code. + Getting Started %Systemroot%\System32\windowspowershell\v1.0\gettingstarted.rtf This is a larger overview of PS, covering the general concepts of shell versus scripting, pipelining objects. The Primer overlaps some of this material (not surprising since Getting Started ideas were stolen for the Primer) but it covers several topics not handled there. It's a bit higher level and is targeted more towards scripting use as well. + PowerShell: The Cheat Sheet %Systemroot%\System32\windowspowershell\v1.0\quadfold.rtf This is one of Jim Truher's children, and an extremely useful one at that. You want to print it out and laminate it. ![]() > Example: while I can find all kinds of great scripting examples in > Powershell, what's the best way to do this in powershell: > "dir /ad /on"? That depends. The following will NOT impress you for brevity, but it has reasons: Get-ChildItem | Where-Object{$_.PSIsContainer} | Sort-Object Name There are several other ways, of course. > (note: I know that I can pipe output to sort for the /on part, but is > that the expected/best way?) Yes. Here's the philosophy in a nutshell - pardon me if you already know this bit. ![]() Experience has shown that for flexible long-term usability, tools that do very specific things very well tend to be the best administrative tools. The best thing does not necessarily include a lot of manipulation such as filtering or sorting. However, a filtering/sorting tool is also a tool, and can then concentrate on doing that particular task very well. This means we can use Get- (Variable|WmiObject|ChildItem|Item|Service|Process|...) to retrieve items, and then use the same tools to filter or sort. There's generally no need to learn command-specific filtering/sorting syntax. There are exceptions to this, of course. For terseness and performance various cmdlets support various schemes for selection, such as wildcarding in name/include/exclude and domain-specific filtering with the -Filter parameter. But for the vast majority of problems, you can just do the following: Get-<Something> | Where-Object{$_.<SomeProperty> -eq <SomeDesiredValue>} | Sort-Object <AnotherProperty> > (note: I haven't gotten the /ad part to work yet, but I was trying to > pipe the get-childitem output to Where, and perform some selection on the > Mode, is that the best way to do it?) That's actually a way I had never considered. But it would work. ![]() My technique depends on the PSIsContainer property of items, which you see if you pipe output into Get-Member. The "mode" is manufactured in the output formatting - if you look at System.IO.DirectoryInfo in %SystemRoot%\system32\windowspowershell\v1.0\types.ps1xml You'll see how it is put together, and that in fact it is simply a string. Where I use this: Where-Object{$_.PSIsContainer} You could instead use: Where-Object{$_.Mode[0] -eq "d"} or Where-Object{$_.Mode -like "d*"} or Where-Object{$_.Attributes -Contains "Directory"} It's also possible to extend the type system with a script snippet. That's a bit beyond what I want to tackle explaining today, but it would let you, for example, add a true/false property to files and directories that is simply "d" for "directory, and then you could do this: Where-Object{$_.d} For now, I think the most obvious compact solution is roughly this, if we use the modes: gci | ?{$_.Mode -like "d*"} | sort name |
My System Specs![]() |
| | #6 (permalink) |
| | Re: powershell and cmd try these dir | ? { 'ReadOnly', 'Archive' -contains $_.attributes } dir | ? { $_.attributes -contains 'ReadOnly', 'Archive' } the first one will return items that are either readonly or archive and the later one will return items that are only BOTH readonly and archive |
My System Specs![]() |
| | #7 (permalink) |
| | Re: powershell and cmd Thank you, Alex for this excellent information. I will be digging in to the guides that you stated in your post. You know, as a developer, one of the reasons that I was very excited about Powershell was the availability of the .Net bcl at the command line. So, I always feel a little chagrined when I find that I could have been using the bcl for something! I suppose that I never noticed before how I "switch modes" when I am thinking about the command line vs. development. I will have to become more accustomed to the blending of the two! Thank you all for your responses to this post. "Alex K. Angelopoulos [MVP]" <aka@online.mvps.org> wrote in message news:ebpXgNJ7GHA.3760@TK2MSFTNGP02.phx.gbl... > > "J.Marsch" <jmarsch@newsgroup.nospam> wrote in message > news:ekmZj396GHA.3644@TK2MSFTNGP03.phx.gbl... >> Although I am enthusiastic about Powershell, I don't get many >> opportunities to use it at work. So, to try to immerse myself in >> Powershell, I have been trying to use it as a replacement for cmd. > > >> Questions: >> Is Powershell intended to be used as a true shell as well as a script >> environment, or should I view it more as a scripting environment that >> happens to be interactive? > > As Klumsy says. This is a "scriptable shell". > >> Is there a good resource for learning the "simple stuff"? > > If the reference documentation doesn't _immediately_ tell you what you > want to know, the very best thing you can do is post your questions here. > This lets us know what's not very simple or intuitive. It also serves as a > reality check for those of us who are off into reflection and dynamic code > generation and PS-based Runge-Kutta analysis. ![]() > > That said, a good starting point is the documents in the $PSHome > directory. > > + The PowerShell Primer > %Systemroot%\System32\windowspowershell\v1.0\userguide.rtf > This is a basic talking walk-through of simple aspects of PS, with quite a > bit of demonstration code. > > + Getting Started > %Systemroot%\System32\windowspowershell\v1.0\gettingstarted.rtf > This is a larger overview of PS, covering the general concepts of shell > versus scripting, pipelining objects. The Primer overlaps some of this > material (not surprising since Getting Started ideas were stolen for the > Primer) but it covers several topics not handled there. It's a bit higher > level and is targeted more towards scripting use as well. > > + PowerShell: The Cheat Sheet > %Systemroot%\System32\windowspowershell\v1.0\quadfold.rtf > This is one of Jim Truher's children, and an extremely useful one at that. > You want to print it out and laminate it. ![]() > > >> Example: while I can find all kinds of great scripting examples in >> Powershell, what's the best way to do this in powershell: >> "dir /ad /on"? > > That depends. The following will NOT impress you for brevity, but it has > reasons: > > Get-ChildItem | Where-Object{$_.PSIsContainer} | Sort-Object Name > > There are several other ways, of course. > >> (note: I know that I can pipe output to sort for the /on part, but is >> that the expected/best way?) > > Yes. Here's the philosophy in a nutshell - pardon me if you already know > this bit. ![]() > > Experience has shown that for flexible long-term usability, tools that do > very specific things very well tend to be the best administrative tools. > The best thing does not necessarily include a lot of manipulation such as > filtering or sorting. However, a filtering/sorting tool is also a tool, > and can then concentrate on doing that particular task very well. > > This means we can use Get- > (Variable|WmiObject|ChildItem|Item|Service|Process|...) to retrieve items, > and then use the same tools to filter or sort. There's generally no need > to learn command-specific filtering/sorting syntax. > > There are exceptions to this, of course. For terseness and performance > various cmdlets support various schemes for selection, such as wildcarding > in name/include/exclude and domain-specific filtering with the -Filter > parameter. But for the vast majority of problems, you can just do the > following: > > Get-<Something> | Where-Object{$_.<SomeProperty> -eq <SomeDesiredValue>} | > Sort-Object <AnotherProperty> > >> (note: I haven't gotten the /ad part to work yet, but I was trying to >> pipe the get-childitem output to Where, and perform some selection on the >> Mode, is that the best way to do it?) > > That's actually a way I had never considered. But it would work. ![]() > > My technique depends on the PSIsContainer property of items, which you see > if you pipe output into Get-Member. The "mode" is manufactured in the > output formatting - if you look at System.IO.DirectoryInfo in > %SystemRoot%\system32\windowspowershell\v1.0\types.ps1xml > You'll see how it is put together, and that in fact it is simply a string. > > Where I use this: > Where-Object{$_.PSIsContainer} > > You could instead use: > Where-Object{$_.Mode[0] -eq "d"} > > or > Where-Object{$_.Mode -like "d*"} > > or > Where-Object{$_.Attributes -Contains "Directory"} > > It's also possible to extend the type system with a script snippet. That's > a bit beyond what I want to tackle explaining today, but it would let you, > for example, add a true/false property to files and directories that is > simply "d" for "directory, and then you could do this: > Where-Object{$_.d} > > For now, I think the most obvious compact solution is roughly this, if we > use the modes: > > gci | ?{$_.Mode -like "d*"} | sort name > > |
My System Specs![]() |
![]() |
| Thread Tools | |
| |