Home » PowerShell » Use PowerShell to Create Empty File

Use PowerShell to Create Empty File

Use the New-Item cmdlet in PowerShell to create an empty file. This command generates an empty file in the specified location.

An empty file can serve multiple purposes such as a placeholder to store temporary data, script execution logs, etc…

In this article, we will discuss how to create an empty file in PowerShell using the New-Item cmdlet.

Create an Empty File Using the New-Item cmdlet

To create an empty file in PowerShell, use the New-Item cmdlet with -ItemType and -Path parameters.

$filePath = "D:\PS\emptyFile.txt" 
New-Item -Path $filePath -ItemType File -Force 

In the above PowerShell script, the New-Item cmdlet uses the -Path parameter to specify the file path and -ItemType as a file to create an empty file at a given location.

The -Force parameter ensures the file is created even if the target directory doesn’t exist. It also ensures to overwrite the file with an empty file if the target folder contains the same file name.

The output of the above script generates an empty file in the specified directory with 0 lengths.

PowerShell Create an Empty File
PowerShell Create an Empty File

Cool Tip: How to check if a file exists in PowerShell!

Generate an Empty File for a Scheduled Task

Let’s consider, you want to write a PowerShell script to create an empty file each day that contains log details about a scheduled task. To generate an empty log file, we will use the New-Item cmdlet.

# Specify the Log Folder Path
$logFolder = "D:\PS"         

# Get the current date with custom format                                                                                    $todayDate = Get-Date -Format "ddMMyyyy" 

# Define the log file name
$logFileName = "rabbitmq_$todayDate.log"

# Join the Log Folder Path and Log File Name
$logFilePath = Join-Path -Path $logFolder -ChildPath $logFileName

# Create an empty log file                                                
New-Item -Path $logFilePath -ItemType File -Force    

In the above PowerShell script, the $logFolder variable contains the log folder path. The script then gets the current date and formats it as a string. The $logFileName variable contains the log file name generated by appending the current date to the base name “rabbitmq_”. The $logFilePath variable stores the full log file location. The New-Item cmdlet creates the empty log file at the specified location.

The output of the above script generates an empty log file as given below:

PS D:\> $logFolder = "D:\PS"                                                                                            
PS D:\> $todayDate = Get-Date -Format "ddMMyyyy"                                                                        PS D:\> $logFileName = "rabbitmq_$todayDate.log"                                                                        PS D:\> $logFilePath = Join-Path -Path $logFolder -ChildPath $logFileName                                               

PS D:\> $logFilePath                                                                                                    D:\PS\rabbitmq_02042023.log

PS D:\> New-Item -Path $logFilePath -ItemType File -Force                                                               

    Directory: D:\PS


Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a----       02-04-2023     19:44              0 rabbitmq_02042023.log


PS D:\>                                                                                                                           

Cool Tip: How to delete the file if exists in PowerShell!

Conclusion

I hope the above article on how to create an empty file in PowerShell using the New-Item cmdlet is helpful to you.

Use the -Force parameter with the New-Item cmdlet to overwrite the file if the file already exists in the specified location.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.