Apidays New York 2024 - The value of a flexible API Management solution for O...
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.