This Video shows how to Test existence of File or Folders in a Path using PowerShell
################################################
DEMO-1
################################################
Prepare Demo Environment
Setting My Working Location
Set-Location "D:\PSTraining\PracticeGround"
Creation of 5 Text test files
1..5|% {New-Item -Path . -Name "$("file" + "$_").txt" -ItemType "file"}
1 Test a path using -Path and -LiteralPath parameter
Test-Path -Path .\*file1.txt
Test-Path -LiteralPath .\file1.txt
2 Test if Syntax is valid
Test-Path -Path ./file6.txt
Test-Path -Path ./file6.txt -IsValid
3 Check whether there exist any specifiedd type of files using -Exclude and -Include parameter
Test-Path -Path .\*.txt
Test-Path -Path .\*.log
Test-Path -Path .\* -Exclude *.txt # txt file will be omitted from evaluation
Test-Path -Path .\* -Include *.txt
4 Check whether there exist any specifiedd type of files using -Filter parameter
Test-Path -Path .\* -Filter *.txt
Test-Path -Path .\* -Filter *.log
5 Check if path leads to File/Folder/RegistryKey or not
Test-Path -Path .\* -PathType Leaf
Test-Path -Path .\new* -PathType Container
Test-Path -Path .\* -PathType any
6 Check paths in the Registry
Test-Path -Path "HKLM:\Software\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell"
7 Test if a file is newer than a specified date
Get-ChildItem .\file1.txt|fl Name,creationtime
Test-Path -Path .\file1.txt -NewerThan "July 16, 2000"
Test-Path -Path .\file1.txt -NewerThan "July 16, 2030"
Test-Path -Path .\file1.txt -OlderThan "July 16, 2000"
Test-Path -Path .\file1.txt -OlderThan "July 16, 2030"
8 Convert the working directory to a standard file system path
Convert-Path .
9 Convert a provider path to a standard registry path
Convert-Path HKLM:\Software\Microsoft
10 Convert a path to a string
Convert-Path ~
################################################
DEMO-2
################################################
Company ABC Host events.. Whenever there is a new event, Concern department upload a file (containing list of Attendees) in a shared folder, IT department fetch this file and take further action like providing access on different Applications and resources.
Now we need a Script that can –
------- Check if file exist or not
------- And If it exist, It should not be an older file. It should have been uploaded today only
##########################################
*************** SOLUTION ******************************
##########################################
New-Item .\Userlist.txt
$file = ".\Userlist.txt"
$Yesterday = (Date).AddDays(-1)
$Existence = Test-Path $file
$TimeValidity = Test-Path $file -NewerThan $Yesterday
If (($Existence) -and ($TimeValidity))
{Write-host "All Well, We are Good to Go with this file" -ForegroundColor Green}
Else
{Write-host "We are not Good to Go" -ForegroundColor Red}
************************************************************************
************************************************************************
Join Me on --
Facebook Group
/ mylearningsonline
Facebook Page
/ mylearningsonline
YouTube Channel
/ mylearningsonline
**************************************************************************
#PowerShell #PowerShellScripting #PowerShellForBeginners #learnPowerShell #PowerShelltraining #PowerShellTutorial #MyLearningsOnline
PowerShell Scripting for Beginners
PowerShell Tutorial for Beginners
PowerShell Full Course
PowerShell Training for Beginners