Clear File or Create New File in PowerShell with Visual Studio Code on Windows - DevPops-Inc/windows GitHub Wiki

• Type $filePath = “< file path >” and press the Enter key twice to declare the $filePath variable and set its value to the desired file path.
image

• Type if (Test-Path -Path $filePath) and press the Enter key to test if the file path exists.
image

• Type { and press the Enter key to open the scope of the if statement.
image

• Type Clear-Content $filePath and press the down arrow to clear contents of the file if it exists.
image

• Press the Enter key to create a new line, type else and press the Enter key to define the else statement.
image

• Type { and press the Enter key to open the scope of the else statement.
image

• Type New-Item $filePath and press the Enter key to create the file if it doesn’t already exist.
image

• Press the Ctrl and S keys to save.
image

• Browse to the desired save location, type script name in the “File name:” box and select “Save.”
image

• Press the Run button.
image

• The file will be created since it didn’t exist previously.
image

⚠️ **GitHub.com Fallback** ⚠️