<<< Back to Search

File Management - Create Text File

Create a text file including some text on it and naming with time stamp to ensure previous created file is not overwritten

Core Snippet and Sample

Copy text below into a power shell session and execute to see the code in action.

# File Management - Create Text File
# Core Snippet and Sample
# www.PowerShellExamples.com

# Get file name based on a timestamp -_-
# This code will create an *.txt file, edit accordingly
$fileName = "MyFile_"+(get-date -format yyyyMMdd_HHmmss) + ".Log"  
# Create Text File -_-
New-Item "C:\Temp\$fileName" -ItemType File -Value "Hello World"

Write-Host "File created: C:\Temp\$fileName"


PS C:\>

Directory: C:\Temp

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----         1/31/2023   9:58 AM             11 MyFile_20230131_095851.Log

File created: C:\Temp\MyFile_20230131_095851.Log