This document provides an overview of using PowerShell to list files on a server. It describes prerequisites like ensuring the correct execution policy is set. The main section provides a PowerShell script that prompts the user for a file path and extension. It checks for files with that extension, displays a message if none are found, and otherwise generates an HTML file listing the files and opens it. The document includes examples of the script output whether files are found or not.
1. Windows PowerShell Basics – List Files
i | P a g e
Table of Contents
Overview.......................................................................................................................................................1
Applies To..................................................................................................................................................1
Pre-Requisites ...........................................................................................................................................1
Current Execution Policy...........................................................................................................................1
PowerShell Script – List File ..........................................................................................................................2
Code Snippet – Check and Delete File ......................................................................................................2
PowerShell Execution – List Files ..........................................................................................................3
PowerShell Output – List Files...............................................................................................................3
PowerShell Output – No Files Found ....................................................................................................4
2. Windows PowerShell Basics – List Files
1 | P a g e
Overview
In this guide we will demonstrate as to how to list files on a server, store and export list to HTML file and
launch the html file.
Applies To
Tested on Windows 10, Windows 2008 R2 and Windows 2012.
Pre-Requisites
Launch PowerShell Command Console or PowerShell ISE.
To run this script, Execution Policy should be set to either of these “AllSigned” or “RemoteSigned” or
“Unrestricted”, you can get current execution policy by running the command; “Get-ExecutionPolicy”.
Each Policy type and its purpose is shown in the below table.
Policy Type Purpose
Restricted No scripts can be run. Windows PowerShell can be used only in interactive mode.
AllSigned Only scripts signed by a trusted publisher can be run.
RemoteSigned Downloaded scripts must be signed by a trusted publisher before they can be run.
Unrestricted No restrictions; all Windows PowerShell scripts can be run.
Current Execution Policy
To know the current run the PowerShell cmdlet; Get-ExecutionPolicy
To list execution policies that can be configured run the PowerShell cmdlet; Get-ExecutionPolicy –List
3. Windows PowerShell Basics – List Files
2 | P a g e
PowerShell Script – List File
In this PowerShell script, we will validate file exists in the folder and if a files are not found, a message box
will be displayed.
Code Snippet – Check and Delete File
The code snippet is for demonstrating “Specific Type of Files Listing in a directory”.
#
# Define HTML Styling
#
$Style = "
<style>
TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
TH{border-width: 2px;padding: 5px;border-style: solid;border-color: green;}
TD{border-width: 2px;padding: 5px;border-style: solid;border-color: black;}
</style> "
#
Clear-Host
[console]::Beep(500,600)
$FilePath=Read-Host ("Enter the Path ")
$FileExtType=Read-Host ("Enter the File Type / Extension ")
if ((Test-Path -Path $FilePath)) {
$OutputFileName="Files_List.html"
Set-Location $FilePath
Push-Location $FilePath
#
# Store File list into a variable for HTML formating
#
$FileListingValidate=(Get-ChildItem "$FilePath" | where {$_.Extension -eq "$FileExtType"}).count
if ( $FileListingValidate -eq 0) {
[console]::Beep(500,600)
[System.Windows.MessageBox]::Show($Filepath + "*" + $FileExtType + ' - No Files Found',"File
Listing")
return
}
$FileListing=Get-ChildItem "$FilePath" | where {$_.Extension -eq "$FileExtType"} | Select-Object
DirectoryName, Name, Length,mode | ConvertTo-Html -Fragment
4. Windows PowerShell Basics – List Files
3 | P a g e
#
# Add header, Body as File Listing
#
ConvertTo-Html -Head $Style -Body $FileListing | Out-File $FilePath$OutputFileName
Invoke-Item $FilePath$OutputFilename
} else {
# Popup Message Box - Invalid Directory
[console]::Beep(500,600)
[System.Windows.MessageBox]::Show($Filepath + ' - Invalid Directory',"Invalid Directory")
return
}
PowerShell Execution – List Files
When script is executed; user is requested to key-in directory and file type that has to be listed, as
shown below.
PowerShell Output – List Files
When script is executed; when the files are found, list will be stored into HTML file and launch it.
5. Windows PowerShell Basics – List Files
4 | P a g e
PowerShell Output – No Files Found
When script is executed, if the files are not found, a message box will be displayed, as shown below;