Thursday, February 2, 2012

Touching a file in Powershell

Although 'touch' being one of the most useful Linux commands, it is still missing in Powershell. The 'touch' command is important for developers and sysadmins alike because it allows you either to update the timestamps of a file, either to create temporary empty filenames.

So here's my Powershell transcription of the bash 'touch' command for those of you who might need it:

param([string] $filetotouch)
if(test-path $filetotouch)
    Set-ItemProperty -Path $filetotouch -Name LastWriteTime -Value (get-date)
    “File $filetotouch timestamp succesfully updated”
    Set-Content -Path ($filetotouch) -Value ($null);
    “File $filetotouch succesfully created”

You can save this file as touch.ps1 then call it from the Powershell command line as follows:

.\touch.ps1 test.txt

I hope you liked this script.


  1. You could also try: Write-Host -NoNewline "" > filename

  2. add-content $FILENAME $null

    That seems to work find for me.

  3. Or just
    "" > filename

    Or is there a difference I'm missing?


Related Posts Plugin for WordPress, Blogger...