Powershell touch

touch filenameOn Windows this is pretty awkward -- commonly I just open up a new circumstances of notepad and also save an empty file.

You watching: Powershell touch

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

I am not looking to specifically complement behaviour of touch, but just to find the most basic feasible tantamount for producing empty documents.


Using the append redirector ">>" resolves the problem wbelow an existing file is deleted:

echo $null >> filename



To develop a empty file:

New-Item -ItemType file example.txtTo update the timestamp of a file:

(gci example.txt).LastWriteTime = Get-Date



Here is a variation that creates a brand-new file if it does not exist or updays 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
In PowerCovering you deserve to develop a comparable Touch attribute as such:

feature touch set-content -Path ($args<0>) -Value ($null) Usage:

touch myfile.txt


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

ft > filenameTo work-related through non-empty records you can use:

ft >> filenameI made a decision this bereason it is a brief command that does nothing in this conmessage, anoop. It is also nice because if you foracquire the redirect:

ft filenamerather of offering you an error, again it just does nothing. Some various other aliasesthat will occupational are Format-Custom (fc) and also Format-Wide (fw).

Tbelow are a bunch of worthy answers currently, however I quite like the alias of New-Item which is just: ni

You can likewise forgo the file kind declaration (which I assume is implicit once an expansion is added), so to produce a javascript file with the name of "x" in my current magazine I can sindicate write:

ni x.js3 chars quicker than touch!

I put together various resources, and wound up via the following, which met my demands. I necessary to collection the compose day of a DLL that was built on a maker in a various timezone:

$upday = get-dateSet-ItemProperty -Path $dllPath -Name LastWriteTime -Value $updateOf course, you deserve to likewise set it for multiple files:

Get-ChildItem *.dll | Set-ItemProperty -Name LastWriteTime -Value $upday
It looks prefer a bunch of the answers here don"t account for file encoding.

See more: How To Uninstall Soda Pdf Application/Software On Your Mac, How To Uninstall Soda Pdf From Your Pc

I just ran into this trouble, for miscellaneous other factors, but

echo $null > $file$null > $fileboth develop a UTF-16-LE file, while

New-Item $file -kind fileproduces a UTF-8 file.

For whatever before factor fc > $file and fc >> $file, additionally seem to create UTF-8 documents.

Out-Documents $file -encoding utf8gives you a UTF-8-BOM file, while

Out-Data $file -encoding asciiprovides you a UTF-8 file. Other valid (yet untested) encodings that Out-Data supports are: <<-Encoding> utf7 >. You can additionally pipe stuff to Out-File to give the file some message information to save, and likewise an -appfinish flag. For example:

echo $null | Out-Documents .stuff.txt -Encoding ascii -Appendthis instance does not upday the timestamp for some reason, but this one does:

echo foo | Out-Documents .stuff.txt -Encoding ascii -AppendAlthough it does have actually the side result of appfinishing "foo" to the finish of the file.

See more: Display Screen Resolution Changes By Itself, Screen Resolution Keeps Changing In Windows 10

If you are unsure around what encoding you have actually, I"ve uncovered VS-Code has a nifty attribute where at the bottom right hand corner it claims what the encoding is. I think Notepad++ likewise has actually a comparable feature.