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
  • 26,555
  • 15
  • 113
  • 235
MmmHmm
  • 738
  • 11
  • 24
  • `new-item .gitignore -itemtype file` seems to work but is kinda clunky to type – MmmHmm Aug 17 '16 at 01:43
  • How can I make an alias that accepts the -itemtype? `new-alias -name touch -value new-item -itemtype file` doesn't work... – MmmHmm Aug 17 '16 at 01:57
  • 1
    Just `new-item .gitignore` works, it assumes you want a file when you're in a hard drive location. – Ƭᴇcʜιᴇ007 Aug 17 '16 at 03:15
  • @Ƭᴇcʜιᴇ007 yes it does, but it'd be nice to just have two words to type in. I'm looking into how to make a PS function so I can add a 'touch' alias to my profile which presumes a type: file. Thanks. – MmmHmm Aug 17 '16 at 03:19
  • 1
    How is "new-item .gitignore" and different than "touch .gitignore"? Since you now know you don't need the "-itemtype", then you should be able to simply alias new-item as "touch". Having said that, "new-item" already has an alias: "ni". So `ni .gitignore` should work fine for what you're asking, and it's even shorter. :) – Ƭᴇcʜιᴇ007 Aug 17 '16 at 03:24
  • @Ƭᴇcʜιᴇ007 "ni"ce :) good to know! The only difference is that `ni` or `new-item` require either `-itemtype file` or answering the `Type:` query - just more keystrokes is all. I'm a PS novice, so partly I am just poking at it trying to learn my way around. I think I have a simple function I can use now tho: `function touch_file {new-item $args[0] -itemtype file} new-alias -name touch -value touch_file` and I can just enter `touch .filename`. Added it to my profile and it appears to work! Yay! – MmmHmm Aug 17 '16 at 03:35
  • The way you get around Windows' not wanting to start a filename with a period is by adding a second period to the end, in this case name the file `.gitignore.`. Windows will recognize that this means you are sure that you want the name to start with a period, and will create the file as `.gitignore` without the second period. – Moshe Katz Aug 17 '16 at 05:16
  • @MosheKatz good to know! This works fine but still requires verbosely including `-itemtype file` with the command or answering the Type query when the command is entered without the specification flag. – MmmHmm Aug 17 '16 at 05:30

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
  • 26,555
  • 15
  • 113
  • 235
MmmHmm
  • 738
  • 11
  • 24