Is there an equivalent of touch in PowerShell?

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?

I am not looking to exactly match behaviour of touch, but just to find the simplest possible equivalent for creating empty files.

  • 1
  • 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, 2012 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, 2014 at 0:49
  • 1
    @jsalonen IOW if you reword the question to 'how to create an empty file in powershell' I'd remove my downvote, but leave *nix touch command out of it. :)
    – yzorg
    Feb 18, 2014 at 14:50
  • 1
    @LưuVĩnhPhúc Clarified the question. I am not looking for feature-complete equivalent of touch, just the matching behaviour for creating empty files.
    – jsalonen
    Sep 21, 2017 at 8:04

14 Answers 14


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

echo $null >> filename
  • 5
    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, 2012 at 21:11
  • 22
    touch is rather different than this if the file already exists
    – jk.
    Nov 7, 2012 at 22:46
  • 32
    echo is unnecessary, $null > filename works great.
    – alirobe
    May 30, 2015 at 12:54
  • 25
    This writes 2 bytes of unicode BOM 0xFEFF for me.
    – mlt
    Apr 20, 2016 at 19:08
  • 8
    Important note: Many unix tools don't deal with BOM. This includes git for example(even on windows). You're gonna have problems if you use files created in this manner with tools that don't recognize BOMs. e.g. you try to create your .gitignore using this command and wonder why it won't work. BOM is the reason.
    – martixy
    Jul 31, 2017 at 22:21

To create a blank file:

New-Item example.txt

Note that in old versions of PowerShell, you may need to specify -ItemType file .

To update the timestamp of a file:

(gci example.txt).LastWriteTime = Get-Date
  • 8
    I think this is the best approach!
    – jysh
    Nov 4, 2013 at 15:30
  • 3
    @riahc3, use this: (gi MyFolder).LastWriteTime = Get-Date . You could use that for files too.
    – dangph
    Jul 28, 2015 at 11:26
  • 39
    Even more pithy: ni example.txt
    – Nick Cox
    Dec 21, 2017 at 1:03
  • 6
    This method doesn't add the awful BOM bytes and the encoding happily appears to be UTF-8.
    – Davos
    Mar 26, 2018 at 10:33
  • 2
    Golfed some more to avoid the interactive dialog @NickCox: ni -i file foo.txt
    – enharmonic
    Mar 27, 2020 at 20:59

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
  • 4
    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. Jan 4, 2014 at 11:59
  • 18
    Very, very tiny quibble: While Touch-File conforms to the Verb-Noun naming convention of PS, Touch is not an "approved" verb (not that it's a significant requirement: msdn.microsoft.com/en-us/library/ms714428.aspx). File is fine, btw. I recommend the names Set-File or Set-LastWriteTime or, my favorite, Update-File. Also, I would recommend Add-Content $file $null instead of echo $null > $file. Finally, set an alias with Set-Alias touch Update-File if you want to keep using the command touch
    – Alan McBee
    May 25, 2017 at 23:36
  • 2
    Suggest adding this as the last line: New-Alias -Name Touch Touch-File
    – stimpy77
    Oct 1, 2019 at 4:33
  • 4
    Nice, but it's better to use New-Item $file instead of echo $null > $file, because the latter will create a 2-byte file with the UTF-16LE BOM in Windows PowerShell (but no longer in PowerShell Core).
    – mklement0
    Nov 5, 2019 at 12:43
  • 3
    This has a TOCTTOU race, the file could be created between the check and the action.
    – poizan42
    Dec 27, 2019 at 13:43

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

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


touch myfile.txt


  • 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, 2012 at 19:34
  • 3
    Add it to your $profile file. (Run notepad $profile to edit that file.)
    – Mark Allen
    Nov 7, 2012 at 21:12
  • 37
    This will delete the contents of the file if it exists.
    – dangph
    Jan 24, 2013 at 6:31
  • 2
    Or the safe version that does not clear out existing file contents function touch { if((Test-Path -Path ($args[0])) -eq $false) { set-content -Path ($args[0]) -Value ($null) } } Sep 11, 2018 at 16:51
  • 3
    ...and the final one-liner version that can also handle directories: function touch {if((Test-Path -Path ($args[0])) -eq $false) {Set-Content -Path ($args[0]) -Value ($null)} else {(Get-Item ($args[0])).LastWriteTime = Get-Date } }
    – not2qubit
    Sep 24, 2020 at 8:05

There are a bunch of worthy answers already, but I quite like the alias of New-Item which is just: ni

You can also forgo the file type declaration (which I assume is implicit when an extension is added), so to create a javascript file with the name of 'x' in my current directory I can simply write:

ni x.js

3 chars quicker than touch!

  • 3
    This is not idempotent ni : The file 'x.js' already exists
    – Zombo
    Nov 30, 2017 at 12:43
  • An idempotent equivalent would be ac x.js $null
    – Nick Cox
    Mar 29, 2020 at 5:02
  • how does this differ from touch? Dec 20, 2020 at 18:45

I prefer Format-Table for this task (mnemonic file touch):

ft > filename

To work with non-empty files you can use:

ft >> filename

I chose this because it is a short command that does nothing in this context, a noop. It is also nice because if you forget the redirect:

ft filename

instead of giving you an error, again it just does nothing. Some other aliases that will work are Format-Custom (fc) and Format-Wide (fw).


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
  • 1
    +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. Feb 25, 2013 at 15:20
  • Without using a variable?
    – riahc3
    Jul 28, 2015 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
  • Thanks! this is the best way Apr 18, 2021 at 5:09

It looks like a bunch of the answers here don't account for file encoding.

I just ran into this problem, for various other reasons, but

echo $null > $file

$null > $file

both produce a UTF-16-LE file, while

New-Item $file -type file

produces a UTF-8 file.

For whatever reason fc > $file and fc >> $file, also seem to produce UTF-8 files.

Out-File $file -encoding utf8

gives you a UTF-8-BOM file, while

Out-File $file -encoding ascii

gives you a UTF-8 file. Other valid (but untested) encodings that Out-File supports are: [[-Encoding] {unknown | string | unicode | bigendianunicode | utf8 | utf7 | utf32 | ascii | default | oem}]. You can also pipe stuff to Out-File to give the file some text data to store, and also an -append flag. For example:

echo $null | Out-File .\stuff.txt -Encoding ascii -Append

this example does not update the timestamp for some reason, but this one does:

echo foo | Out-File .\stuff.txt -Encoding ascii -Append

Although it does have the side effect of appending "foo" to the end of the file.

If you are unsure about what encoding you have, I've found VS-Code has a nifty feature where at the bottom right hand corner it says what the encoding is. I think Notepad++ also has a similar feature.

  • Set-Content -Path $file -value $null does the job and it does not affect file encoding. Check also ss64 version of touch. Mar 14, 2017 at 15:18
  • 1
    Yes, in Windows PowerShell $null > $file unfortunately creates a 2-byte file with the UTF-16LE encoding; fortunately, in PowerShell Core you now get a truly empty file, as you do with New-Item in both editions. It is not meaningful to speak of a truly empty file (length of 0 bytes) as having a specific character encoding, such as UTF-8, however. Character encoding is only meaningful with respect to content (which is missing here), and, more specifically, with respect to text content.
    – mklement0
    Nov 5, 2019 at 13:04
ac file.txt $null

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


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.

  • 2
    Thanks! What does sc mean? Edit: figured it out ("Set Content")
    – jsalonen
    Feb 27, 2013 at 15:00

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

fsutil file createnew file.name 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)


I used the name "Write-File" because "Touch" isn't an approved PowerShell verb. I still alias it as touch, however.


  Creates a new file or updates the modified date of an existing file.

 .Parameter Path
  The path of the file to create or update.
Function Write-File {
       [Parameter( Mandatory=$True, Position=1 )]
       [string] $Path,
       [switch] $WhatIf,
       [System.Management.Automation.PSCredential] $Credential
    $UseVerbose = $PSCmdlet.MyInvocation.BoundParameters['Verbose'].IsPresent -eq $True
    $UseDebug = $PSCmdlet.MyInvocation.BoundParameters['Debug'].IsPresent -eq $True
    $TimeStamp = Get-Date
    If( -Not [System.Management.Automation.WildcardPattern]::ContainsWildcardCharacters( $Path ) ) {
        New-Item -ItemType:File -Verbose:$UseVerbose -Debug:$UseDebug -WhatIf:$WhatIf -Credential $Credential -Path $Path -ErrorAction SilentlyContinue -Confirm:$False | Out-Null
    Set-ItemProperty -Verbose:$UseVerbose -Debug:$UseDebug -WhatIf:$WhatIf -Credential $Credential -Path $Path -Name LastWriteTime -Value:$TimeStamp -Confirm:$False | Out-Null

Set-Alias -Name touch -Value Write-File

Export-ModuleMember -Function Write-File
Export-ModuleMember -Alias touch


Import-Module ./Touch.psm1
touch foo.txt


  • Paths in other directories
  • Credential for network paths
  • Verbose, Debug, and WhatIf flags
  • wildcards (timestamp update only)
  • 1
    The New-Item command has been offered in four previous answers.  You have provided a 20-line wrapper for it.  Can you explain a bit more clearly what advantage your solution has over the earlier ones?  For example, what are these Verbose, Debug, and WhatIf flags, etc? Mar 23, 2017 at 4:08
  • 1
    One important difference between this answer and New-Item is that this updates the timestamp of existing files.
    – jpaugh
    Jul 26, 2018 at 15:49
  • For some reason, I have to always import the module again and again, this shouldn't happen, right Import-Module ./Touch.psm1
    – shirish
    Nov 4, 2019 at 21:15
  • 1
    @shirish You need to import modules every session, yes, unless you put it in a folder in the PSModulePath. (Or add its folder to PSModulePath.) Feb 28, 2020 at 20:36

The webpage http://xahlee.info/powershell/PowerShell_for_unixer.html 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.

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