SlideShare a Scribd company logo
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
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
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
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.
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;

More Related Content

PDF
How To List Files on Remote Server - PowerShell
PDF
How To Setup SSH Keys on CentOS 7
PDF
How To Check and Delete a File via PowerShell
PDF
How To Connect to Active Directory User Validation
PDF
How To Connect To Active Directory PowerShell
PDF
Windows PowerShell Basics – How To Create powershell for loop
PDF
Zimbra Troubleshooting - Mails not being Delivered or Deferred or Connection ...
PDF
Install and Configure RSyslog – CentOS 7 / RHEL 7
How To List Files on Remote Server - PowerShell
How To Setup SSH Keys on CentOS 7
How To Check and Delete a File via PowerShell
How To Connect to Active Directory User Validation
How To Connect To Active Directory PowerShell
Windows PowerShell Basics – How To Create powershell for loop
Zimbra Troubleshooting - Mails not being Delivered or Deferred or Connection ...
Install and Configure RSyslog – CentOS 7 / RHEL 7

What's hot (20)

PDF
Windows PowerShell Basics - How To List PSDrive Info
PDF
How To Install and Configure SNMP on RHEL 7 or CentOS 7
PDF
How To List Nginx Modules Installed / Complied on CentOS 7
PDF
How To Install and Configure Log Rotation on RHEL 7 or CentOS 7
PDF
How To Configure Apache VirtualHost on RHEL 7 on AWS
PDF
How To Check IE Enhanced Security Is Enabled Windows PowerShell
PDF
How To Install and Configure Open SSH Server on Ubuntu
PDF
How To Create PowerShell Function
PDF
How To Install and Configure Apache SSL on CentOS 7
PDF
How To Check file exists and Delete PowerShell
PDF
Bash Script - How To Monitor Application Error Logs and Send Notification
PDF
How to Fix Duplicate Packages in YUM on CentOS 7
PDF
How to Install and Configure Cacti on Linux
PDF
How To Disable IE Enhanced Security Windows PowerShell
PDF
How To Manage Linux User on RHEL 7
PDF
Shell Script Disk Usage Report and E-Mail Current Threshold Status
PDF
How To Install and Configure GNome on CentOS 7
PDF
How To Configure FirewallD on RHEL 7 or CentOS 7
PDF
How To Install OpenFire in CentOS 7
PDF
How To View Current Execution Policy PowerShell
Windows PowerShell Basics - How To List PSDrive Info
How To Install and Configure SNMP on RHEL 7 or CentOS 7
How To List Nginx Modules Installed / Complied on CentOS 7
How To Install and Configure Log Rotation on RHEL 7 or CentOS 7
How To Configure Apache VirtualHost on RHEL 7 on AWS
How To Check IE Enhanced Security Is Enabled Windows PowerShell
How To Install and Configure Open SSH Server on Ubuntu
How To Create PowerShell Function
How To Install and Configure Apache SSL on CentOS 7
How To Check file exists and Delete PowerShell
Bash Script - How To Monitor Application Error Logs and Send Notification
How to Fix Duplicate Packages in YUM on CentOS 7
How to Install and Configure Cacti on Linux
How To Disable IE Enhanced Security Windows PowerShell
How To Manage Linux User on RHEL 7
Shell Script Disk Usage Report and E-Mail Current Threshold Status
How To Install and Configure GNome on CentOS 7
How To Configure FirewallD on RHEL 7 or CentOS 7
How To Install OpenFire in CentOS 7
How To View Current Execution Policy PowerShell
Ad

Similar to How To List Files and Display In HTML Format (20)

PPTX
Getting Started With PowerShell Scripting
PPTX
Power Shell for System Admins - By Kaustubh
PDF
One man loves powershell once he failed
PPTX
PowerShell - Be A Cool Blue Kid
PPTX
Power shell training
PPTX
Introduction to windows power shell in sharepoint 2010
PPTX
PowerShell-1
PDF
WORKING WITH FILE AND PIPELINE PARAMETER BINDING
PPTX
PowerShell 101
PDF
Basic commands for powershell : Configuring Windows PowerShell and working wi...
PPT
PowerShell Core Skills (TechMentor Fall 2011)
PPT
NIIT ISAS Q5 Report - Windows PowerShell
PPTX
Windows power shell basics
PPTX
2016 spice world_london_breakout
PPT
Why and How Powershell will rule the Command Line - Barcamp LA 4
PDF
Windows PowerShell Step by Step 3rd Edition Wilson
PDF
Introduction to PowerShell
PPTX
02Introduction to Power Shell Win Server all in one.pptx
PPTX
Get-Help: An intro to PowerShell and how to Use it for Evil
PDF
Windows Powershell Step By Step 3rd Edition Wilson Ed
Getting Started With PowerShell Scripting
Power Shell for System Admins - By Kaustubh
One man loves powershell once he failed
PowerShell - Be A Cool Blue Kid
Power shell training
Introduction to windows power shell in sharepoint 2010
PowerShell-1
WORKING WITH FILE AND PIPELINE PARAMETER BINDING
PowerShell 101
Basic commands for powershell : Configuring Windows PowerShell and working wi...
PowerShell Core Skills (TechMentor Fall 2011)
NIIT ISAS Q5 Report - Windows PowerShell
Windows power shell basics
2016 spice world_london_breakout
Why and How Powershell will rule the Command Line - Barcamp LA 4
Windows PowerShell Step by Step 3rd Edition Wilson
Introduction to PowerShell
02Introduction to Power Shell Win Server all in one.pptx
Get-Help: An intro to PowerShell and how to Use it for Evil
Windows Powershell Step By Step 3rd Edition Wilson Ed
Ad

More from VCP Muthukrishna (13)

PDF
How To Construct IF and Else Conditional Statements
PDF
How To Create PowerShell Function Mandatory Parameter and Optional Parameter
PDF
How To Create Power Shell Function Mandatory Parameter Value
PDF
How To Configure Nginx Load Balancer on CentOS 7
PDF
Nginx bind() to 0.0.0.0:9080 failed
PDF
How To Install and Configure Screen on CentOS 7
PDF
How To Install and Configure Salt Master on Ubuntu
PDF
How To Protect SSH Access with Fail2Ban on RHEL 7
PDF
How To Configure SNMP Logging on RHEL 7
PDF
How To Find Package Installation Date on RHEL 7
PDF
How to Upgrade Openfire on CentOS 7
PDF
How To Reset root Password on CentOS 7
PDF
How To Install and Use ABRT CLI on RHEL 7
How To Construct IF and Else Conditional Statements
How To Create PowerShell Function Mandatory Parameter and Optional Parameter
How To Create Power Shell Function Mandatory Parameter Value
How To Configure Nginx Load Balancer on CentOS 7
Nginx bind() to 0.0.0.0:9080 failed
How To Install and Configure Screen on CentOS 7
How To Install and Configure Salt Master on Ubuntu
How To Protect SSH Access with Fail2Ban on RHEL 7
How To Configure SNMP Logging on RHEL 7
How To Find Package Installation Date on RHEL 7
How to Upgrade Openfire on CentOS 7
How To Reset root Password on CentOS 7
How To Install and Use ABRT CLI on RHEL 7

Recently uploaded (20)

PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
KodekX | Application Modernization Development
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Empathic Computing: Creating Shared Understanding
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Electronic commerce courselecture one. Pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
Cloud computing and distributed systems.
Chapter 3 Spatial Domain Image Processing.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
20250228 LYD VKU AI Blended-Learning.pptx
KodekX | Application Modernization Development
Reach Out and Touch Someone: Haptics and Empathic Computing
NewMind AI Weekly Chronicles - August'25 Week I
Dropbox Q2 2025 Financial Results & Investor Presentation
Encapsulation_ Review paper, used for researhc scholars
Empathic Computing: Creating Shared Understanding
“AI and Expert System Decision Support & Business Intelligence Systems”
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Building Integrated photovoltaic BIPV_UPV.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Spectral efficient network and resource selection model in 5G networks
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Electronic commerce courselecture one. Pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
Understanding_Digital_Forensics_Presentation.pptx
Cloud computing and distributed systems.

How To List Files and Display In HTML Format

  • 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;