Home > Software engineering >  If statements in Powershell
If statements in Powershell

Time:02-23

I want to check if file.txt exists. If it exists delete it and reinstall it. If it does not exist download it.

Test-Path -Path file.txt -PathType Leaf
Remove-Item 'file.txt'
Invoke-WebRequest -Uri http://example.com/file.txt -OutFile file.txt

I can't seem to figure out how to put this into an if statement (I am new to Powershell).

CodePudding user response:

if (Test-Path -Path file.txt -PathType Leaf){
    Remove-Item file.txt
    Invoke-WebRequest -Uri http://example.com/file.txt -OutFile file.txt
}
else{
    Invoke-WebRequest -Uri http://example.com/file.txt -OutFile file.txt
}
  • Related