PowerShell: Enumerate Files recursive and get full path for each file

To enumerate all files of a specific path recursive and get the full path for each file using Microsoft PowerShell:


Get-ChildItem -rec | ForEach-Object -Process {$_.FullName}

to capture the output and generate an XML file do:


Get-ChildItem -rec | ForEach-Object -Process {$_.FullName} | Export-Clixml c:\temp\report.xml

to only enumerate / list files, use the psIsContainer switch:


Get-ChildItem -rec | Where-object {!$_.psIsContainer -eq $true} | ForEach-Object -Process {$_.FullName}

or for folders only:


Get-ChildItem -rec | Where-object {!$_.psIsContainer -eq $false} | ForEach-Object -Process {$_.FullName}

Search for files using PowerShell

To search for files using Windows PowerShell is pretty easy.


PS C:\> CD $Env:WinDir
PS C:\Windows>Get-ChildItem -Recurse | Where-Object {$_.Name -match "notepad.exe"}

this should result in something like.


Directory: Microsoft.PowerShell.Core\FileSystem::C:\Windows

Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 21.01.2008 03:46 169472 notepad.exe

to supress any access denied errors, you can add the -errorAction “SilentlyContinue” instruction / parameter.


PS C:\> CD $Env:WinDir
PS C:\Windows> Get-ChildItem -Recurse -errorAction "SilentlyContinue" | Where-Object {$_.Name -match "notepad.exe"}

and if you want to have the result exported to an Excel CSV file, you could simply add Export-Csv c:\temp\output.csv


PS C:\> CD $Env:WinDir
PS C:\Windows> Get-ChildItem -Recurse -errorAction "SilentlyContinue" | Where-Object {$_.Name -match "notepad.exe"} | Export-Csv c:\temp\output.csv

PowerShell: UAC, SUDO, Elevation, Access to the path ‘C:\Program Files’ is denied.

Powershell on Windows Vista and Windows 7 runs restricted and not-elevated by default. Microsoft did a good job when they invented the so called UAC (User Account Control). As ususal, Microsoft started something new and did not finish it yet – the product is still not feature-complete. Well, probably it’s their philosophy to create thousands of products in a vertical manner, the user then is left all alone finding out how to dive into the features from a horizontal perspective…

What i mean: PowerShell runs restricted and has NO SUDO command. For sake of christ, there is a workaround.

First, find out the location of your profile.ps1. it easy as:


PS C:\> $profile
C:\Users\

Probably this script does not exist by default. At least on my systems this was the case. So create it.
When created / if it does exist do:


PS C:\> notepad.exe $profile

This should open your profile.ps1 script in notepad..
Edit this Script to include:


function elevate-process
{
$file, [string]$arguments = $args;
$psi = new-object System.Diagnostics.ProcessStartInfo $file;
$psi.Arguments = $arguments;
$psi.Verb = "runas";
$psi.WorkingDirectory = get-location;
[System.Diagnostics.Process]::Start($psi);
}

set-alias sudo elevate-process;

Now you’re almost done. After starting a new PowerShell session – even a non-elevated one – you can run commands elevated by using the sudo command.
For Illustration:

PS C:\> sudo notepad.exe

If the new PowerShell session should display a warning regarding the execution of scripts, check out my older posts (using the site search), i’ve postet a solution for that.

Solution: Microsoft.PowerShell_profile.ps1 cannot be loaded because the execution of scripts is disabled on this system. Please see “get-help about_signing” for more details.

Solution: Microsoft.PowerShell_profile.ps1 cannot be loaded because the execution of scripts is disabled on this system. Please see “get-help about_signing” for more details.

Check the current execution-policy

PS C:\Windows\System32> Get-ExecutionPolicy
Restricted
PS C:\Windows\System32>

To change the current execution-policy use:
Note: this command must be run elevated (Start Powershell using ‘Run as Administrator’)

PS C:\Windows\System32> Set-Executionpolicy -ExecutionPolicy Unrestricted