PowerShell equivalents to some dir command variations

L

Larry__Weiss

I'm thinking that I can learn some things about PowerShell if I had some PowerShell
expressions to study that were the equivalent of some cmd.exe dir command
invocations
for a file system directory.

What is a PowerShell equivalent to these dir commands that I use often?

dir /ad
dir /a-d
dir /od
dir /od /a-d
dir /ah
dir /a-h
dir /b

- Larry
 

My Computer

L

Larry__Weiss

Marco Shaw [MVP] wrote:

> Larry__Weiss wrote:

>> I'm thinking that I can learn some things about PowerShell if I had
>> some PowerShell expressions to study that were the equivalent
>> of some cmd.exe dir command invocations for a file system directory.
>> What is a PowerShell equivalent to these dir commands that I use often?
>> dir /ad
>> dir /a-d
>> dir /od
>> dir /od /a-d
>> dir /ah
>> dir /a-h
>> dir /b
>>
>
> Take a gander here:
> http://blogs.msdn.com/powershell/archive/2009/03/13/dir-a-d.aspx
> If you're up for trying v2, you're in for some fun! ;-)
>

I tried to follow the two links in that article
https://connect.microsoft.com/feedback/ViewFeedback.aspx?SiteID=99&FeedbackID=252549&wa=wsignin1.0
https://connect.microsoft.com/feedback/ViewFeedback.aspx?SiteID=99&FeedbackID=308796&wa=wsignin1.0

but I get the ambiguous notice
Page Not Found
The content that you requested cannot be found or you do not have permission
to view it.

Is there a way I can sign up to be able to access these resources?

- Larry
 

My Computer

B

Bob Landau

Using Get-ChildItem to do slighly complicated but common filesystem tasks was
sooo frustrating that I nearly gave up using Powershell. What you've
mentioned below is so ackward compared to the NT shell.

Luckily I didn't give up on PS and wrote my own version of DIR which is/was
up on Microsofts' Scripting site (dir2.ps1) unfortunately I can't find it now
to give you the location.

I think I'll stick it up on CodePlex (the scripting site is really meant for
10-20 lines). Perhaps thats why the got rid if it.

Its a wrapper around Get-ChildItem but gives you the same simple syntax that
you and me are acustom to. See man page below

If you a would like to receive a copy prior it being published send me email
via my dog Galen: [email protected] (he doesn't mind spam) and I send you
the file. Its much too large to post to this news group.

bob

Here is the helpfile for it.

PS C:\Windows\System32> dir -?

These are the Standard options available in DIR

/A Displays files with specified attributes.
attributes D Directories R Read-only files
H Hidden files A Files ready for archiving
S System files I Not content indexed files
L Reparse Points - Prefix meaning not
/B Uses bare format (no heading information or summary)
/O List by files in sorted order
sortorder N By name (alphabetic) S By size (smallest first)
E By extension (alphabetic) D By date/time (oldest first)
A LastAccessTime C Same as D
W LastWriteTime - Prefix to reverse order
/P Pauses after each screenful of information
/S Displays files in specified directory and all subdirectories
/W Uses wide list format




-Search <character expression to include in search> <optional character
expression to exclude in search>

Like Get-ChildItem/Dir: the current directory will be used if no
-Path parameter is specified

This option is similar to the WHERE command that some might be
familar with.
This is simply a short cut for the following

Get-ChildItem -Path <...> -Include <...> -Exclude < > -Recurse
Both -Path and -Exclude are optional.

-LiteralPath now will filter the output by processing the expressions in
both Include/Exclude arguments.

This workaround can be removed once -LiteralPath has been fixed.

-Escape < [System.Collections.HashTable[]] > with the following signature:

@{Name='Name'; Expression=' User defined Filter pipe : returning
either a File/Directory Info class '}

This gives one the ability to inject a pipe expression into this
script.

This option is really ONLY benefitial to 5 percent of the Pipeline
expressions. Unless this is
used to "filter" the output for the next Pipe; this should not be
used.



Press <RETURN> for Get-ChildItem help :

< the rest of the help is Get-ChildItem>

"Larry__Weiss" wrote:

> Marco Shaw [MVP] wrote:

> > Larry__Weiss wrote:

> >> I'm thinking that I can learn some things about PowerShell if I had
> >> some PowerShell expressions to study that were the equivalent
> >> of some cmd.exe dir command invocations for a file system directory.
> >> What is a PowerShell equivalent to these dir commands that I use often?
> >> dir /ad
> >> dir /a-d
> >> dir /od
> >> dir /od /a-d
> >> dir /ah
> >> dir /a-h
> >> dir /b
> >>
> >
> > Take a gander here:
> > http://blogs.msdn.com/powershell/archive/2009/03/13/dir-a-d.aspx
> > If you're up for trying v2, you're in for some fun! ;-)
> >
>
> I tried to follow the two links in that article
> https://connect.microsoft.com/feedback/ViewFeedback.aspx?SiteID=99&FeedbackID=252549&wa=wsignin1.0
> https://connect.microsoft.com/feedback/ViewFeedback.aspx?SiteID=99&FeedbackID=308796&wa=wsignin1.0
>
> but I get the ambiguous notice
> Page Not Found
> The content that you requested cannot be found or you do not have permission
> to view it.
>
> Is there a way I can sign up to be able to access these resources?
>
> - Larry
>
>
 

My Computer

willsteele

New Member
Being a cmd shell junkie, I can understand your pain. I know it's an old post, but, these are good commands for folks coming up from cmd to powershell anyway.

dir /ad

Get-ChildItem | Where-Object {$_.PSIsContainer}

dir /a-d

Get-ChildItem | Where-Object {!$_.PSIsContainer}

dir /od

Get-ChildItem | Sort-Object Date

dir /od /a-d

Get-ChildItem | Where-Object {!$_.PSIsContainer} | Sort-Object Date

dir /ah

Get-ChildItem -force -path C:\ | Where-Object {$_.Mode -match "h"}

dir /a-h

Get-ChildItem -force -path C:\ | Where-Object {!$_.Mode -match "h"}

dir /b

Get-ChildItem | Select-Object Name
 

My Computer

L

Larry__Weiss

What I've also done is to place a cmd.exe batch file named dirr.bat into my path
that contains the two lines

@echo off
dir %*

This lets me add an extra "r" to "dir" whenever I need to use the legacy form of
the dir command.

Bob Landau shared his dir-like PowerShell command with me some time ago, and I
intend to complete a derivative of it that permits a similar parameter structure
as the legacy dir command without falling back on cmd.exe

I feel like I now have enough PowerShell practice behind me to allow me to
understand how Bob's scripting works.

I also feel that for reasons of ease of use as well as efficiency that Microsoft
should extend the file system provider's version of Get-ChildItem to allow the
parameter structure of cmd.exe's dir command.

- Larry


willsteele wrote:

> Being a cmd shell junkie, I can understand your pain. I know it's an
> old post, but, these are good commands for folks coming up from cmd to
> powershell anyway.
>
> dir /ad
>
> Get-ChildItem | Where-Object {$_.PSIsContainer}
>
> dir /a-d
>
> Get-ChildItem | Where-Object {!$_.PSIsContainer}
>
> dir /od
>
> Get-ChildItem | Sort-Object Date
>
> dir /od /a-d
>
> Get-ChildItem | Where-Object {!$_.PSIsContainer} | Sort-Object Date
>
> dir /ah
>
> Get-ChildItem -force -path C:\ | Where-Object {$_.Mode -match "h"}
>
> dir /a-h
>
> Get-ChildItem -force -path C:\ | Where-Object {!$_.Mode -match "h"}
>
> dir /b
>
> Get-ChildItem | Select-Object Name
>
>
 

My Computer

ewgoforth

New Member
Being a cmd shell junkie, I can understand your pain. I know it's an old post, but, these are good commands for folks coming up from cmd to powershell anyway.

dir /ad

Get-ChildItem | Where-Object {$_.PSIsContainer}

dir /a-d

Get-ChildItem | Where-Object {!$_.PSIsContainer}

dir /od

Get-ChildItem | Sort-Object Date

dir /od /a-d

Get-ChildItem | Where-Object {!$_.PSIsContainer} | Sort-Object Date

dir /ah

Get-ChildItem -force -path C:\ | Where-Object {$_.Mode -match "h"}

dir /a-h

Get-ChildItem -force -path C:\ | Where-Object {!$_.Mode -match "h"}

dir /b

Get-ChildItem | Select-Object Name
I know this is ancient, but it's the first result in a Bing Query. I found that I needed to change Sort-Object Date to Sort-Object LastWriteTime to mimic the /od functionality.
 

My Computer

System One

  • Operating System
    Windows 10
Top