Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 awkward -- 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
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 '14 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 '14 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 '14 at 14:44

11 Answers 11

up vote 60 down vote accepted

Using the append redirector ">>" resolves the issue where an existing file is deleted:

echo $null >> filename
share|improve this answer
Ok. I have modified the ans , now it should do it in one line – Yash Agarwal Nov 7 '12 at 19:53
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
touch is rather different than this if the file already exists – jk. Nov 7 '12 at 22:46
perhaps echo $null >> filename would be more similar to Unix touch. – Nathan May 21 '14 at 20:11
echo is unnecessary, $null > filename works great. – alirobe May 30 '15 at 12:54

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
        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 '14 at 11:59
Thank you for posting this answer. Excellent . – Aluan Haddad Jul 17 '15 at 16:32

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! – Jayesh Bhoot Nov 4 '13 at 15:30
Does this work for updating the timestamp of a folder? – riahc3 Jul 28 '15 at 6:31
@riahc3, use this: (gi MyFolder).LastWriteTime = Get-Date . You could use that for files too. – dangph Jul 28 '15 at 11:26

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

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


touch myfile.txt


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
Add it to your $profile file. (Run notepad $profile to edit that file.) – Mark Allen Nov 7 '12 at 21:12
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
Without using a variable? – riahc3 Jul 28 '15 at 6:33

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

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
Thanks! What does sc mean? Edit: figured it out ("Set Content") – jsalonen Feb 27 '13 at 15:00

I prefer

fc > filename

for this task. To work with non-empty files you can use

fc >> filename

fc is simply an alias for Format-Custom. I chose it because it is a short command that does nothing in this context, a noop. It is also nice because if you forget the redirect

fc filename

instead of giving you an error, again it just does nothing. Some other aliases that will work are

ft -> Format-Table
fw -> Format-Wide
share|improve this answer

The webpage suggests:

new-item -type file [filename]

and this does indeed create a new file of size zero.

This doesn't perform the other function of Unix touch, namely to update the timestamp if filename already exists, but the question implies that the user just wants to create a zero-sized file interactively without resorting to Notepad.

share|improve this answer

to create an empty file in windows, the fastes way is the following:

fsutil file createnew 0

The zero is filesize in bytes, so this is also useful to create large file (they will not be useful for testing compression since they do not contain actual data and will compress down to pretty much nothing)

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .