Home » PowerShell » PowerShell Write Date to File

PowerShell Write Date to File

In PowerShell, it provides a Get-Date cmdlet to get the current date and time and Out-File and other cmdlets to write the date and time to a file.

The Out-File cmdlet in PowerShell sends the output to a file.

In this article, we will discuss how to retrieve the date and time and output the date time to the file in PowerShell.

Date and Time Output to a File

To send the current date and time output to a file, we can use the Get-Date and Out-File cmdlets:

$demoFile = "D:\PS\aduser_backup.log"

Get-Date | Out-File -FilePath $demoFile

In the above PowerShell script, the $demoFile holds the file path. The Get-Date cmdlet retrieves the current date and time of the systems and pipes it to the Out-File cmdlet to output the date to a file.

The output of the above script writes the current date and time to the specified file in the default date format.

PowerShell Write Date to a File
PowerShell Write Date to a File

Write Customized Date and Time Format to File

You can use the -Format parameter with the Get-Date cmdlet to customize the date and time format and write the date to a file using the Out-File command.

$demoFile = "D:\PS\aduser_backup.log"                                                                           $dateFormat = "yyyy-MM-dd HH:mm:ss"                                                                             (Get-Date).ToString($dateFormat) | Out-File -FilePath $demoFile                                                 Get-Content -Path $demoFile  

In the above PowerShell script, variable $demoFile contains the file path. $dateFormat variable stores the customized date format. Using the ToString() method over the Get-Date command with the format, it formats the date and time and passes the output to the Out-File command to write the date and time to a file.

The output of the above PowerShell script writes the current date and time to a file.

PS C:\> $demoFile = "D:\PS\aduser_backup.log"                                                                           PS C:\> $dateFormat = "yyyy-MM-dd HH:mm:ss"                                                                             PS C:\> (Get-Date).ToString($dateFormat) | Out-File -FilePath $demoFile                                                 

PS C:\> Get-Content -Path $demoFile                                                                                     2023-04-09 23:17:05

PS C:\>                                                                                                                                                                                                                                                                                                                                                                               

Use Out-File -Append Date and Time to an Existing File

To append the date and time to an existing file, use the -Append parameter with the Out-File cmdlet.

$demoFile = "D:\PS\aduser_backup.log"                                                                           $dateFormat = "yyyy-MM-dd HH:mm:ss"
                                                                             
(Get-Date).ToString($dateFormat) | Out-File -FilePath $demoFile -Append                                                Get-Content -Path $demoFile  

In the above PowerShell script, the Out-File cmdlet uses the parameter -Append to output date time to log file.

Conclusion

I hope the above article on how to use PowerShell to write the date to a file using the Get-Date and Out-File cmdlets is useful to you.

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