SlideShare a Scribd company logo
2
Most read
3
Most read
4
Most read
Windows PowerShell Basics – List Files Remote Server
i | P a g e
Table of Contents
Overview.......................................................................................................................................................1
Applies To..................................................................................................................................................1
Pre-Requisites ...........................................................................................................................................1
Current Execution Policy...........................................................................................................................1
PowerShell Script – List File – Remote Server...............................................................................................2
PowerShell Code Snippet – List Files ........................................................................................................2
PowerShell Execution – User Input.......................................................................................................3
PowerShell Output – List Files...............................................................................................................3
PowerShell Output – Path Not Found...................................................................................................4
PowerShell Output – Server Unreachable ............................................................................................4
PowerShell Output – File List................................................................................................................5
Windows PowerShell Basics – List Files Remote Server
1 | P a g e
Overview
In this post / guide we will demonstrate, listing files on a remote server’s in a specific directory.
Applies To
Tested on Windows 10, Windows 2008 R2 and Windows 2012.
Pre-Requisites
1) This script has to been run on domain controller, so that all the servers are accessible without
administrator password authentication.
2) 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 Remote Server
2 | P a g e
PowerShell Script – List File – Remote Server
This script will read drive and folder from user input, we will list files on a remote server in a specific folder
and drive based on user input. In this script we will limit the listing only to first five files only.
PowerShell Code Snippet – List Files
The code snippet is for demonstrating “List Files on a remote server on a specific drive”.
Clear-Host
Set-Location $env:USERPROFILE
Push-Location $env:USERPROFILE
#
# Set Output file Location
#
$OutPutFilename=$env:USERPROFILE+"DesktopFileLists.txt"
$ServersFilename=$env:USERPROFILE+"DesktopServerNames.txt"
$ReadDrive=Read-Host "Enter the Drive"
#
# Remove string :
#
$DriveName=($ReadDrive).Contains(":")
if ($DriveName=$true) {
$ReadDrive=$ReadDrive.Remove(1,2)
}
$ReadDirectory=Read-Host "Enter the Directory to List (exclude drive)"
#
# Remove Output file if file already exists.
#
if (Test-Path $OutPutFilename) {
Remove-Item $OutPutFilename
}
$AuthroizedUser=Get-Credential
Get-Content $ServersFilename | ForEach-Object {
if (-Not (Test-Connection -ComputerName $_ -Count 1 -Quiet)) {
Write-Host Server - $_ `t Is currently down or Unreachable -ForegroundColor White -
BackgroundColor Red
return
} else {
Write-Host `nServer - $_ `t Is Reachable -ForegroundColor Green -BackgroundColor Black
if (Test-Path -Path $_$ReadDrive$$ReadDirectory) {
$GCCount=(Get-Childitem "$_$ReadDrive$$ReadDirectory" -Filter *.txt).count
if ($GCCount -ge 0) {
Write-Host Searching .... "$_$ReadDrive$$ReadDirectory"
Write-Host File listing count is $GCCount -ForegroundColor Green -BackgroundColor Black
Windows PowerShell Basics – List Files Remote Server
3 | P a g e
Get-Childitem "$_$ReadDrive$$ReadDirectory" -Filter *.txt | select @{n='ComputerName';
e={_$}}, FullName, LastAccessTime | select -first 5 | Format-Table -AutoSize | Out-File -Append
$OutPutFilename
}
} else {
Write-Host "`n$_ - Path Not Found `n" -BackgroundColor Red -ForegroundColor Yellow
}
return
}
}
Invoke-Item $OutPutFilename
PowerShell Execution – User Input
When script is executed; when the files are found, list will be saved into file a text file and subsequently
file will be launched, after completion of the script execution.
PowerShell Output – List Files
When script is executed, if the files are found script will list files to a text file.
Windows PowerShell Basics – List Files Remote Server
4 | P a g e
PowerShell Output – Path Not Found
When script is executed, if the files are not found, a message will be displayed, as shown below;
PowerShell Output – Server Unreachable
When script is executed, if server is unreachable or down, a message will be displayed, as shown below;
Windows PowerShell Basics – List Files Remote Server
5 | P a g e
PowerShell Output – File List
When script is executed, if the files are found script will list files to a text file.

More Related Content

PPTX
Data saturday Oslo Azure Purview Erwin de Kreuk
PDF
How To List Files and Display In HTML Format
PPTX
Powering up on PowerShell - BSides Greenville 2019
PPTX
Getting Started With PowerShell Scripting
PDF
Basic commands for powershell : Configuring Windows PowerShell and working wi...
PPTX
Powering up on power shell avengercon - 2018
PPTX
PowerShell - Be A Cool Blue Kid
PDF
Powering up on PowerShell - BSides Charleston - Nov 2018
Data saturday Oslo Azure Purview Erwin de Kreuk
How To List Files and Display In HTML Format
Powering up on PowerShell - BSides Greenville 2019
Getting Started With PowerShell Scripting
Basic commands for powershell : Configuring Windows PowerShell and working wi...
Powering up on power shell avengercon - 2018
PowerShell - Be A Cool Blue Kid
Powering up on PowerShell - BSides Charleston - Nov 2018

Similar to How To List Files on Remote Server - PowerShell (20)

PDF
Power on, Powershell
PDF
One man loves powershell once he failed
PPT
NIIT ISAS Q5 Report - Windows PowerShell
PPTX
Introduction to windows power shell in sharepoint 2010
PPTX
PowerShell for Cyber Warriors - Bsides Knoxville 2016
PDF
Power shell examples_v4
PPTX
Power Shell for System Admins - By Kaustubh
PPTX
Power shell training
PPT
PowerShell Core Skills (TechMentor Fall 2011)
PPTX
Powershell Tech Ed2009
PPTX
CCI2019 - I've got the Power! I've got the Shell!
PPTX
Windows power shell basics
PPSX
Sunil phani's take on windows powershell
TXT
An a z index of windows power shell commandss
PPTX
Introduction to powershell
PPTX
Get-Help: An intro to PowerShell and how to Use it for Evil
PPTX
PowerShell-1
PDF
WORKING WITH FILE AND PIPELINE PARAMETER BINDING
PDF
How To Check and Delete a File via PowerShell
PPTX
learn ps new advance for all powershell.pptx
Power on, Powershell
One man loves powershell once he failed
NIIT ISAS Q5 Report - Windows PowerShell
Introduction to windows power shell in sharepoint 2010
PowerShell for Cyber Warriors - Bsides Knoxville 2016
Power shell examples_v4
Power Shell for System Admins - By Kaustubh
Power shell training
PowerShell Core Skills (TechMentor Fall 2011)
Powershell Tech Ed2009
CCI2019 - I've got the Power! I've got the Shell!
Windows power shell basics
Sunil phani's take on windows powershell
An a z index of windows power shell commandss
Introduction to powershell
Get-Help: An intro to PowerShell and how to Use it for Evil
PowerShell-1
WORKING WITH FILE AND PIPELINE PARAMETER BINDING
How To Check and Delete a File via PowerShell
learn ps new advance for all powershell.pptx
Ad

More from VCP Muthukrishna (20)

PDF
How to Fix Duplicate Packages in YUM on CentOS 7
PDF
How To Install and Configure GNome on CentOS 7
PDF
How To Connect to Active Directory User Validation
PDF
How To Connect To Active Directory PowerShell
PDF
Zimbra Troubleshooting - Mails not being Delivered or Deferred or Connection ...
PDF
How To Setup SSH Keys on CentOS 7
PDF
How To Install and Configure Open SSH Server on Ubuntu
PDF
Windows PowerShell Basics - How To List PSDrive Info
PDF
How To List Nginx Modules Installed / Complied on CentOS 7
PDF
Windows PowerShell Basics – How To Create powershell for loop
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 Create PowerShell Function
PDF
How To Disable IE Enhanced Security Windows PowerShell
PDF
How To Check IE Enhanced Security Is Enabled Windows PowerShell
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
How to Fix Duplicate Packages in YUM on CentOS 7
How To Install and Configure GNome on CentOS 7
How To Connect to Active Directory User Validation
How To Connect To Active Directory PowerShell
Zimbra Troubleshooting - Mails not being Delivered or Deferred or Connection ...
How To Setup SSH Keys on CentOS 7
How To Install and Configure Open SSH Server on Ubuntu
Windows PowerShell Basics - How To List PSDrive Info
How To List Nginx Modules Installed / Complied on CentOS 7
Windows PowerShell Basics – How To Create powershell for loop
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 Create PowerShell Function
How To Disable IE Enhanced Security Windows PowerShell
How To Check IE Enhanced Security Is Enabled Windows PowerShell
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
Ad

Recently uploaded (20)

PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Approach and Philosophy of On baking technology
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPT
Teaching material agriculture food technology
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Encapsulation theory and applications.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Machine learning based COVID-19 study performance prediction
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Electronic commerce courselecture one. Pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Spectral efficient network and resource selection model in 5G networks
Dropbox Q2 2025 Financial Results & Investor Presentation
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Approach and Philosophy of On baking technology
Review of recent advances in non-invasive hemoglobin estimation
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Programs and apps: productivity, graphics, security and other tools
Digital-Transformation-Roadmap-for-Companies.pptx
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Teaching material agriculture food technology
Diabetes mellitus diagnosis method based random forest with bat algorithm
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Encapsulation theory and applications.pdf
20250228 LYD VKU AI Blended-Learning.pptx
Machine learning based COVID-19 study performance prediction
“AI and Expert System Decision Support & Business Intelligence Systems”
Electronic commerce courselecture one. Pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...

How To List Files on Remote Server - PowerShell

  • 1. Windows PowerShell Basics – List Files Remote Server i | P a g e Table of Contents Overview.......................................................................................................................................................1 Applies To..................................................................................................................................................1 Pre-Requisites ...........................................................................................................................................1 Current Execution Policy...........................................................................................................................1 PowerShell Script – List File – Remote Server...............................................................................................2 PowerShell Code Snippet – List Files ........................................................................................................2 PowerShell Execution – User Input.......................................................................................................3 PowerShell Output – List Files...............................................................................................................3 PowerShell Output – Path Not Found...................................................................................................4 PowerShell Output – Server Unreachable ............................................................................................4 PowerShell Output – File List................................................................................................................5
  • 2. Windows PowerShell Basics – List Files Remote Server 1 | P a g e Overview In this post / guide we will demonstrate, listing files on a remote server’s in a specific directory. Applies To Tested on Windows 10, Windows 2008 R2 and Windows 2012. Pre-Requisites 1) This script has to been run on domain controller, so that all the servers are accessible without administrator password authentication. 2) 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 Remote Server 2 | P a g e PowerShell Script – List File – Remote Server This script will read drive and folder from user input, we will list files on a remote server in a specific folder and drive based on user input. In this script we will limit the listing only to first five files only. PowerShell Code Snippet – List Files The code snippet is for demonstrating “List Files on a remote server on a specific drive”. Clear-Host Set-Location $env:USERPROFILE Push-Location $env:USERPROFILE # # Set Output file Location # $OutPutFilename=$env:USERPROFILE+"DesktopFileLists.txt" $ServersFilename=$env:USERPROFILE+"DesktopServerNames.txt" $ReadDrive=Read-Host "Enter the Drive" # # Remove string : # $DriveName=($ReadDrive).Contains(":") if ($DriveName=$true) { $ReadDrive=$ReadDrive.Remove(1,2) } $ReadDirectory=Read-Host "Enter the Directory to List (exclude drive)" # # Remove Output file if file already exists. # if (Test-Path $OutPutFilename) { Remove-Item $OutPutFilename } $AuthroizedUser=Get-Credential Get-Content $ServersFilename | ForEach-Object { if (-Not (Test-Connection -ComputerName $_ -Count 1 -Quiet)) { Write-Host Server - $_ `t Is currently down or Unreachable -ForegroundColor White - BackgroundColor Red return } else { Write-Host `nServer - $_ `t Is Reachable -ForegroundColor Green -BackgroundColor Black if (Test-Path -Path $_$ReadDrive$$ReadDirectory) { $GCCount=(Get-Childitem "$_$ReadDrive$$ReadDirectory" -Filter *.txt).count if ($GCCount -ge 0) { Write-Host Searching .... "$_$ReadDrive$$ReadDirectory" Write-Host File listing count is $GCCount -ForegroundColor Green -BackgroundColor Black
  • 4. Windows PowerShell Basics – List Files Remote Server 3 | P a g e Get-Childitem "$_$ReadDrive$$ReadDirectory" -Filter *.txt | select @{n='ComputerName'; e={_$}}, FullName, LastAccessTime | select -first 5 | Format-Table -AutoSize | Out-File -Append $OutPutFilename } } else { Write-Host "`n$_ - Path Not Found `n" -BackgroundColor Red -ForegroundColor Yellow } return } } Invoke-Item $OutPutFilename PowerShell Execution – User Input When script is executed; when the files are found, list will be saved into file a text file and subsequently file will be launched, after completion of the script execution. PowerShell Output – List Files When script is executed, if the files are found script will list files to a text file.
  • 5. Windows PowerShell Basics – List Files Remote Server 4 | P a g e PowerShell Output – Path Not Found When script is executed, if the files are not found, a message will be displayed, as shown below; PowerShell Output – Server Unreachable When script is executed, if server is unreachable or down, a message will be displayed, as shown below;
  • 6. Windows PowerShell Basics – List Files Remote Server 5 | P a g e PowerShell Output – File List When script is executed, if the files are found script will list files to a text file.