Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

In PowerShell is there an equivalent of touch?

For instance in Linux I can create a new empty file by invoking:

touch filename

On Windows this is pretty ackward -- usually I just open a new instance of notepad and save an empty file.

So is there a programmatic way in PowerShell to do this?

share|improve this question
1  
    
Thanks. I looked at them, but most of the answer focus on command-prompt. I'd like to have a PowerShell solution that doesn't require me to install new applications. –  jsalonen Nov 7 '12 at 19:33
    
Downvoted the question - both features are only a few more lines of code, just implement both, not just half, esp. when the missing half other command is so dangerous. –  yzorg Feb 18 at 0:49
    
@yzorg: What do you mean by both features? I was only asking how to create an empty file in PS the way you can do with touch in Linux. –  jsalonen Feb 18 at 9:13
    
@jsalonen Use *nix touch on an existing file it will update the last write time without modifying the file, see the links from @amiregelz. This question has a high google ranking for powershell touch, I wanted to alert copy/paste-ers that just this half of it can destroy data, when *nix touch doesn't. See @LittleBoyLost answer that handles when the file already exists. –  yzorg Feb 18 at 14:44

8 Answers 8

up vote 23 down vote accepted

Try this:

echo $null > filename
share|improve this answer
    
Sorry it doesn't. It gives me something like cmdlet Copy-Item at command pipeline position 1 Supply values for the following parameters: –  jsalonen Nov 7 '12 at 19:37
    
just press enter , it will give some error but ignore that , your file will be created. –  Yash Agarwal Nov 7 '12 at 19:38
    
Actually you are right, it works. However, it's annoying to work that way if you have to run this many times. –  jsalonen Nov 7 '12 at 19:39
2  
Also known as 'echo null>filename' from a command prompt, possibly a batch file. Cool to see the PowerShell version of it, thanks! –  Mark Allen Nov 7 '12 at 21:11
8  
touch is rather different than this if the file already exists –  jk. Nov 7 '12 at 22:46

Here is a version that creates a new file if it does not exist or updates the timestamp if it does exist.

Function Touch-File
{
    $file = $args[0]
    if($file -eq $null) {
        throw "No filename supplied"
    }

    if(Test-Path $file)
    {
        (Get-ChildItem $file).LastWriteTime = Get-Date
    }
    else
    {
        echo $null > $file
    }
}
share|improve this answer
    
This is the correct answer for replicating the Unix touch program (albeit with a different name), but the question is oriented to simply creating a new file. –  Jamie Schembri Jan 4 at 11:59

In PowerShell you can create a similar Touch function as such:

function touch {set-content -Path ($args[0]) -Value ($null)} 

Usage:

touch myfile.txt

Source

share|improve this answer
    
This is great, thanks! Just what I wanted! Any ideas how I could install this function into the PowerShell so that it loads automatically when I start the shell? –  jsalonen Nov 7 '12 at 19:34
2  
Add it to your $profile file. (Run notepad $profile to edit that file.) –  Mark Allen Nov 7 '12 at 21:12
8  
This will delete the contents of the file if it exists. –  dangph Jan 24 '13 at 6:31

I put together various sources, and wound up with the following, which met my needs. I needed to set the write date of a DLL that was built on a machine in a different timezone:

$update = get-date
Set-ItemProperty -Path $dllPath -Name LastWriteTime -Value $update

Of course, you can also set it for multiple files:

Get-ChildItem *.dll | Set-ItemProperty -Name LastWriteTime -Value $update
share|improve this answer
    
+1 for the most Powershell-ish way to change LastWriteTime on a file (which is what I needed), though the question focused on the new file creation feature of the touch command. –  Nathan Hartley Feb 25 '13 at 15:20
    
+1 Just what I needed. –  Garett May 8 '13 at 15:24

To create a blank file:

New-Item -ItemType file example.txt

To update the timestamp of a file:

(gci example.txt).LastWriteTime = Get-Date
share|improve this answer
    
I think this is the best approach! –  Ghost Nov 4 '13 at 15:30

For the scenario you described (when the file doesn't exist), this is quick and easy:

PS> sc example.txt $null

However, the other common use of touch is to update the file's timestamp. If you try to use my sc example that way, it will erase the contents of the file.

share|improve this answer
1  
Thanks! What does sc mean? Edit: figured it out ("Set Content") –  jsalonen Feb 27 '13 at 15:00

Open your profile file:

notepad $profile

Add the following line:

function touch {New-Item "$args" -ItemType File}

Save it and reload your $profile in order to use it straight away. (No need to close and open powershell)

. $profile

To add a new file in the current directory type:

touch testfile.txt

To add a new file inside 'myfolder' directory type:

touch myfolder\testfile.txt

If a file with the same name already exists, it won't be overidden. Instead you'll get an error.

I hope it helps

Bonus tip:

You can make the equivalent of 'mkdir' adding the following line:

function mkdir {New-Item "$args" -ItemType Directory} 

Same use:

mkdir testfolder
mkdir testfolder\testsubfolder
share|improve this answer
ac file.txt $null

Won't delete the file contents but it won't update the date either.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.