In PowerShell v 3.0 the
Get-ChildItem cmdlet has new parameters. You can list
just folders (and not files) by appending -Directory. I also like the -Attributes parameter,
which can
filter system or hidden files.
Note 2: This example uses GCI, which is the famous
alias for Get-ChildItem.
Note 3: You could swap -File for -Directory and
contrast the results.
Note 4: You could specify the path with $Env:WinDir
instead of using WMI.
Guy
Recommends: WMI Monitor and It's Free!
Windows Management Instrumentation (WMI) is one of the hidden
treasures of Microsoft operating systems. Fortunately, SolarWinds
have created a
Free WMI Monitor so that you can discover these gems of performance
information, and thus improve your PowerShell scripts. Take the guess work out of which WMI counters to use when scripting the
operating system, Active Directory or Exchange Server.
DOS experts found PowerShell 2.0 frustrating because there were
commands you could do with DIR that you could not achieve with GCI.
Now that deficiency has been rectified, for instance see how easily you
can filter lists of files by attribute.
Note 4: Whereas PowerShell cmdlets all have
singular nouns, plurals crop-up amongst the parameters. My point,
-Attributes is plural and this led me to believe you could filter on
multiple attributes.
# PowerShell 3.0 GCI -Attributes using and Get-ChildItem C:\
-Attributes hidden+ReadOnly
Note 5: I also stumbled across the handy 'Not'
operator, it was the exclamation mark (!). However, you have to
precede it with the and operator, thus (+!) Here is an actual
example:
# PowerShell 3.0 GCI -Attributes and Not Get-ChildItem C:\ -Attributes
hidden+!ReadOnly
Note 6: This displays files that are hidden but
NOT read only.
Thanks to the new PowerShell intellisense you can easily research
parameters for cmdlets such as Get-ChildItem.
The secret is to type the cmdlet's name slowly, and watch-out for
little context menus to appear on screen. For example, see what
happened when I typed GCI C:\windows\system32, and then as I typed a dash
-
PowerShell v 2.0 lacks the Attributes parameter thus it's more difficult
to list files based on their System, Hidden or ReadOnly value. One
way would be to use a Where-Object clause, but PowerShell 3.0's -Attribute
method is much more efficient.
#PowerShell 2.0 Method of listing files based on attribute
Get-ChildItem C:\ -force | Where {$_.Mode -like '*s*' }
Guy Recommends: A Free Trial of the Network Performance Monitor
(NPM)
SolarWinds'
Orion performance monitor
will help you discover what's happening on your network. This
utility will also guide you through troubleshooting; the dashboard will
indicate whether the root cause is a broken link, faulty equipment or
resource overload.
Perhaps the NPM's best feature is the way it suggests solutions to network
problems. Its
second best feature is the ability to monitor the health of individual VMware
virtual machines. If you are interested in troubleshooting, and creating network maps, then I recommend that you take advantage of SolarWinds' offer.
Note 8: The point is we can see there is a file
property called 'Extension'. Incidentally GM (Get-Member) reveals properties
that you don't see in explorer, for example, CreationTimeUtc.
Scenario: We want a list of the dynamic link library
(.dll) files in the System32 folder.
Method: We employ a Where-Object (alias 'Where')
clause.
# PowerShell 3.0 File Properties Get-Childitem Where Get-Childitem C:\Windows\System |
Where Extension -eq .dll
Following the above example you would think that | Where
CreationTime.Year -gt 2010 would provide a list of files that were born in
2011, but it doesn't.
# PowerShell 3.0 Where Problem Get-Childitem C:\Windows\System | Where
CreationTime.Year -gt 2010
Note 9: Although the above example completes without
error it does not display any files. Just in case there are no
matching
files, change -gt to -lt. Still no files appear.
Solution: Revert to the 'Old' PowerShell v 2.0 Where syntax, complete
with {Curly brackets}, $_. and 'single speech mark'.
Import users from a spreadsheet. Just provide a list of the
users with their fields in the
top row, and save as .csv file. Then launch this FREE utility and match
your fields with AD's
attributes, click to import the users. Optionally, you can
provide the name of the OU where the new accounts will be born.
There are also two bonus tools in this free download, and all 3 have been approved by Microsoft:
With Microsoft, there are always at least three ways of doing
everything, what seems like redundancy when you are an expert, seems like
perspective when you are a beginner. Get-ChildItem has not one, but
three aliases check thus:
# PowerShell Alias GCI Get-Alias
-definition Get-ChildItem
Summary of PowerShell 3.0 Get-ChildItem New Features
PowerShell 3.0 brings has extra parameters to many cmdlets, for example
DOS lovers missed the equivelent of Dir *. in PowerShell v 2.0's
Get-ChildItem. I also like the -Attributes parameter to
filter ReadOnly files. Learn more for yourself through the
Intellisense context sensitive menus.
If you like this page then please share it with your friends
Windows Management Instrumentation (WMI) is one of the hidden
treasures of Microsoft operating systems.
Fortunately, SolarWinds
have created the
Free WMI Monitor so that you can actually see and understand these gems of
performance information. Take the guess work out of which
WMI counters to use for applications like Microsoft Active Directory,
SQL or Exchange Server.