Useful PowerShell Scriptlets for Files and Folders

  • Scripting
  • Published Sep 15, 2009 Updated Oct 27, 2010

What is the best way to learn PowerShell? Never use cmd.exe again!

With PowerShell 2.0 on my Windows 7 machine I decided it finally is time to polish my rusty knowledge of the language dating back to the days when PowerShell was still called Monad. In my attempt to re-familiarize myself with PoSh I consciously use it for tasks that would take me mere seconds with other tools like cmd.exe, or even old friends like Perl. But, hey, learning takes time, and there is nothing better than practice! Here are a couple of things I have learned en route.

Searching for a Particular File

Long version:

Get-ChildItem c:\ -Include advapi32.dll -Recurse -Force -ErrorAction SilentlyContinue | Select-Object FullName

Short version:

gci c:\ advapi32.dll -r -fo -ea silentlycontinue | select fullname

This recursively scans drive C: for the file advapi32.dll, looks at hidden and system files (-Force) and ignores errors that might be caused by insufficient permissions. For each file found, the full path is displayed.

Sample Output:

FullName
--------
C:\Windows\System32\advapi32.dll
C:\Windows\SysWOW64\advapi32.dll
C:\Windows\winsxs\amd64_microsoft-windows-advapi32_31bf3856ad364e35_6.1.7600.16385_none_3f3d4351a032bf57\advapi32.dll
C:\Windows\winsxs\x86_microsoft-windows-advapi32_31bf3856ad364e35_6.1.7600.16385_none_e31ea7cde7d54e21\advapi32.dll

Cmd.exe equivalent:

dir /a /s /b c:\advapi32.dll

Listing Encrypted Files and Folders

Long version:

Get-ChildItem c:\ -Include * -Recurse -Force -ErrorAction SilentlyContinue | Where-Object {$_.Attributes -ge "Encrypted"} | Select-Object FullName

Short version:

gci c:\ * -r -fo -ea silentlycontinue | ? {$_.attributes -ge "encrypted"} | select fullname

This iterates over all objects on drive C: and displays the full path of those that have the encrypted attribute set.

Cmd.exe equivalent:

None (without resorting to external tools, which would be possible with PowerShell, too).

Improvements?

Is there a more elegant way of doing this? If so, please let me know by posting a comment.

Update 1: Thanks to Shay Levy I replaced -gt with -ge in the search for encrypted files. Now the scriptlet also finds files that only have the encrypted attribute set.

Comments

Related Posts

Blocking Office Macros, Managing Windows & macOS via Intune

Blocking Office Macros, Managing Windows & macOS via Intune
How to centrally manage essential security settings of self-managed devices This is a guest post by Martin Kretzschmar, customer success engineer at vast limits, the uberAgent company. One thing I especially like about my everyday working life is the flexibility it offers. I appreciate the freedom of choice in terms of location, time and device. We want to avoid getting into micro-management but, being an IT company, we also need to provide the necessary security where needed.
Scripting

Latest Posts

Fast & Silent 5 Watt PC: Minimizing Idle Power Usage

Fast & Silent 5 Watt PC: Minimizing Idle Power Usage
This micro-series explains how to turn the Lenovo ThinkCentre M90t Gen 6 into a smart workstation that consumes only 5 Watts when idle but reaches top Cinebench scores while staying almost imperceptibly silent. In the first post, I showed how to silence the machine by replacing and adding to Lenovo’s CPU cooler. In this second post, I’m listing the exact configuration that achieves the lofty goal of combining minimal idle power consumption with top Cinebench scores.
Hardware

Fast & Silent 5 Watt PC: Lenovo ThinkCentre M90t Modding

Fast & Silent 5 Watt PC: Lenovo ThinkCentre M90t Modding
This micro-series explains how to turn the Lenovo ThinkCentre M90t Gen 6 into a smart workstation that consumes only 5 Watts when idle but reaches top Cinebench scores while staying almost imperceptibly silent. In this first post, I’m showing how to silence the machine by replacing and adding to Lenovo’s CPU cooler. In a second post, I’m listing the exact configuration that achieves the lofty goal of combining minimal idle power consumption with top Cinebench scores.
Hardware