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.
•
• Type if (Test-Path -Path $filePath)
and press the Enter key to test if the file path exists.
•
• Type {
and press the Enter key to open the scope of the if
statement.
•
• Type Clear-Content $filePath
and press the down arrow to clear contents of the file if it exists.
•
• Press the Enter key to create a new line, type else and press the Enter key to define the else statement.
•
• Type {
and press the Enter key to open the scope of the else
statement.
•
• Type New-Item $filePath
and press the Enter key to create the file if it doesn’t already exist.
•
• Press the Ctrl and S keys to save.
•
• Browse to the desired save location, type script name in the “File name:” box and select “Save.”
•
• Press the Run button.
•
• The file will be created since it didn’t exist previously.
•