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”
    }
else
    {
    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.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...