Create file if not exists with name as today’s date using PowerShell

Hey folks today in this PowerShell tutorial, we will discuss How to create a file with today’s date using PowerShell? How to create a file, if the file does not exist? How to write to a text file using PowerShell?

We will also discuss how to check if a file exists using PowerShell, if not exists it will create the file and add content to it. Else it will add content to the existing file using PowerShell.

How to create a file with today’s date using PowerShell?

Here we are generating a text file with a file name as today’s date in dd-mm-yyyy format.

You can write the below PowerShell script by using visual studio code or PowerShell ISE.

$FileName = (Get-Date).tostring("dd-MM-yyyy")
$FolderPath="E:\Desktop"
New-Item -itemType File -Path $FolderPath -Name ($FileName + ".txt")

Once you run the code, you will see a file will be generated like below:

Create file using PowerShell
Create file using PowerShell

How to create a file if the file does not exist?

But next time if you will run the code, it will show the file is already exists. But you might need to add one condition like if the file does not exist then it will create a file. So the PowerShell command will be like below:

$FileName = (Get-Date).tostring("dd-MM-yyyy")
$FolderPath="E:\Desktop"
$Path = $FolderPath+"\"+ $FileName+".txt"
if (!(Test-Path $Path))
{
New-Item -itemType File -Path $FolderPath -Name ($FileName + ".txt")
}
else
{
Write-Host "File already exists"
}

The above code will create a file if the does not exist else it will display a message like below (File already exists):

Create a File If Not Exists via PowerShell
Create a File If Not Exists via PowerShell

PowerShell check if file exists

Now, we will see how to check if a file exists or not? If the file exists, then we will add content to the existing file using PowerShell else it will create the file and add some content to it.

if (!(Test-Path "E:\Folder1\demo.txt"))
{
New-Item -path E:\Folder1 -name demo.txt -type "file" -value "This a new file created from PowerShell"
}
else
{
Add-Content -path E:\Folder1\demo.txt -value "The file already exists, just adding the content"
}

New-Item PowerShell cmdlets help to create a new file or folder.

Add-Content PowerShell cmdlets help to add content to the file.

The output comes like below:

powershell check if file exists
PowerShell check if file exists
PowerShell check if file exists
PowerShell check if file exists

How to write to text file using PowerShell?

Now if you want to write to the text file you can add the below PowerShell command like below:

"This is demo message" | Add-Content $Path

Here the Path is the file path.

You can see the file content like below:

Write to file using PowerShell
Write to file using PowerShell

PowerShell check if file exists and delete

Below is the PowerShell script to check if the file exists and delete. The PowerShell script will check if the file exists and if the file exists it will delete the file.

Remove-Item, PowerShell command helps to delete/remove file from the folder.

$filename="E:\Folder1\demo.txt"
If (Test-Path $filename){
Remove-Item $filename
}

You may like following PowerShell tutorials:

Hope this article will be helpful to check if a file exists in PowerShell. Also, Create the file if not exists with the name as today’s date using PowerShell.

Also, we learned, how to write to a text file using PowerShell?

>