1

How can I use the touch command to make an ".ignore" file from PowerShell?

I can use the command in the git Bash (MINGW64)

me@my_computer MINGW64 ~/stuff (master)
$ touch .gitignore

...but it would be great to stay in PowerShell and work around Windows desire to not have filenames which start with a period. The Bash help/man/info pages do not recognize touch. FWIW, I am following along with this tutorial and did some research with the links in the answers here but have not found an answer. When I try touch .gitignore in PowerShell I get this error msg:

PS C:\Users\_user_name_\stuff> touch .gitignore
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:6
+ touch <<<<  .gitignore
    + CategoryInfo          : ObjectNotFound: (touch:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
phuclv
  • 30,396
  • 15
  • 136
  • 260
MmmHmm
  • 768

1 Answers1

1

Adding this code to my .ps1 profile does the trick: C:\Users\user_name\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

<#
The following function and alias assignment
are for use with git to create files which
start with a period, e.g. .gitignore
#>
function touch_file
{new-item $args[0] -itemtype file}
new-alias -name touch -value touch_file

And now I can enter touch .gitignore into the PowerShell prompt and not have Windows balk at my filename starting with a period or asking me to tell what type of object it is making:

PS C:\Users\user_name> touch .gitignore
    Directory: C:\Users\user_name
Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         8/16/2016  11:41 PM          0 .gitignore

One step closer to getting my lappy to respond adequately to "Do what I want!" :)

phuclv
  • 30,396
  • 15
  • 136
  • 260
MmmHmm
  • 768