root
root

Reputation: 137

If statements in Powershell

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).

Upvotes: 0

Views: 109

Answers (1)

Paweł Kaczanowski
Paweł Kaczanowski

Reputation: 26

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
}

Upvotes: 1

Related Questions