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:

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

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:


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:

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:
- Working with PowerShell Variables
- PowerShell reference variable
- Working with PowerShell Date Command (Get-Date)
- PowerShell Basics: A Beginners Guide
- Top 51 PowerShell Examples You Should Learn
- Delete list items created before N days using PowerShell in SharePoint Online
- Retrieve List Items Created in Last N days using PowerShell SharePoint Online
- SharePoint Online check if File exists or not in a document library using PowerShell
- How to create and use PowerShell global variable
- PowerShell find files modified in last N days
- PowerShell find files modified in last 24 hours and PowerShell get last modified time of files in a folder
- Exception calling ExecuteQuery with 0 argument(s): Unable to connect to the remote server
- How to create an array in PowerShell from CSV file
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?
I am Bijay a Microsoft MVP (8 times –Â My MVP Profile) in SharePoint and have more than 15 years of expertise in SharePoint Online Office 365, SharePoint subscription edition, and SharePoint 2019/2016/2013. Currently working in my own venture TSInfo Technologies a SharePoint development, consulting, and training company. I also run the popular SharePoint website EnjoySharePoint.com