Powershell: `touch` not work

Created on 11 Jan 2019  Â·  12Comments  Â·  Source: PowerShell/PowerShell

According to this guide, by default, Powershell support an alias touch to create a new empty file (touch test.txt == New-Item -Path test.txt)
But it doesn't work so I created this issue.

Steps to reproduce

PS> touch reamde.md

Expected behavior

readme.md file created in my dir 

Actual behavior

PS C:\Users\longnx\Documents> touch readme.md
touch : The term 'touch' is not recognized as the name of a cmdlet, function, script file, or operable program. Check
the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ touch readme.md
+ ~~~~~
    + CategoryInfo          : ObjectNotFound: (touch:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Environment data


Name                           Value
----                           -----
Name                           Value
----                           -----
PSVersion                      6.1.1
PSEdition                      Core
GitCommitId                    6.1.1
OS                             Microsoft Windows 10.0.16299
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
Issue-Question Resolution-Answered

Most helpful comment

BTW this doesn't mean that you can't create this alias if you'd like. Just put this in your profile script:

New-Alias touch New-Item

All 12 comments

touch is a Linux command from what I can find, not a PowerShell command or a Windows command.

Is there a reason you'd expect such a command to work?

For creating an empty file you can use New-Item readme.md.

touch is a Linux command from what I can find, not a PowerShell command or a Windows command.

Is there a reason you'd expect such a command to work?

according to this guide they have created an alias "touch" default for PowerShell

For creating an empty file you can use New-Item readme.md.

I know this, sorry for not mention that they leave touch in learning so I created this issue.

Well yeah, but they mention that in the context of Bash's touch test.txt can be done from PS with New-Item -Path test.txt. That said, touch on Linux does more than create an empty file. If the file exists, it will update the last write time IIRC. New-Item doesn't do that so it isn't a "direct" replacement for touch.

The guide appears to simply be there for reference, indeed; it just indicates how to accomplish common tasks in Bash with PS commands instead.

ls , clear, mkdir... works :( but not touch , so sad :(
if this shouldnot work, I think this issue could be closed.

thanks @rkeithhill and @vexx32

You can close it. In general, the PS team got in a bit of trouble with *nix aliases they created that masked the actual corresponding utility on Linux. Remember, aliases take precedence over executables. And anyway, in the case of aliasing touch to New-Item, it would let folks down who expect an existing file's timestamp to be updated.

BTW this doesn't mean that you can't create this alias if you'd like. Just put this in your profile script:

New-Alias touch New-Item

use git bash for touch to work, as its a linux command

BTW this doesn't mean that you can't create this alias if you'd like. Just put this in your profile script:

New-Alias touch New-Item

Thanks man .. It works

BTW this doesn't mean that you can't create this alias if you'd like. Just put this in your profile script:

New-Alias touch New-Item

↑ This is the best way!!! Do this once, and use touch always.

Was this page helpful?
0 / 5 - 0 ratings