0

I find powershell in some ways, can do regexes to a limited extent. No doubt it can do more but I can't see why it isn't here. I'm trying to use a regex to specify files/directories.

For example, when I have a directory called 'qwe' this will show it

PS C:\Users\bob\a> Get-ChildItem C:\Users\bob\a\[qwe][qwe][qwe]

d----        24/08/2014   1:28 PM            qwe

But I can't do

PS C:\Users\bob\a> Get-ChildItem C:\Users\bob\a\q.e

(to pick up the qwe directory)

And if I do

PS C:\Users\bob\a> Get-ChildItem C:\Users\bob\a\..

Then it goes back a directory and there's no way I can find to make it match a file/dir of 2+ characters or even 2 characters, with the regex ..

However, it does support this aspect of regular expressions [abcd] (a character class to match an 'a' or 'b' or 'c' or 'd') or [a-z] though it is somewhat unregex like as [abcd] only matches where the result is one character. With a proper regex it'd normally match anything with that character including when there's more than that character there.

1
  • 1
    These [qwe] are not regular expressions. This is part of globbing syntax (though results are same as in regular expressions). In globbing a way to specify 'any single character' is ?. Try: Get-ChildItem c:\users\bob\a\q?e (en.wikipedia.org/wiki/Glob_(programming))
    – BartekB
    Aug 31, 2014 at 18:43

1 Answer 1

1

Dot has a special meaning for file names, so you have to use Where-Object (or its short form) for such type of filtering:

Get-ChildItem . | Where-Object { $_.Name -match '^q.e$' }
3
  • testing that on the machine i'm currently on. Get-ChildItem . returns the directory, ok. But Get-ChildItem . | Where-Object { $_Name -match 'a' } or even Get-ChildItem . | Where-Object { $_Name -match '.' } (which should return everything 'cos any file/dir is at least one character.. but it returns nothing. Any time there's a | Where-Object...... after it I get nothing returned
    – barlop
    Aug 30, 2014 at 22:18
  • example- pastebin.com/raw.php?i=kq757JUA
    – barlop
    Aug 30, 2014 at 22:19
  • You've missed a '.' (dot) between $_ and Name. Aug 30, 2014 at 22:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .