How to add a touch function to PowerShell

By | May 7, 2019

I really like the mac/*nix ability to use touch to create a new file:

I wanted to be able to do it in Windows PowerShell – here’s how you do it:

Creating a temp function

Run the following command from the PowerShell terminal:

function touch {New-Item -ItemType File -Name ($args[0])}

Making it permanent

This is just slightly trickier. *IF* you already have a profile you need to add the function to the profile. How do you know if you already have a profile?

Test-Path $PROFILE

If that command comes back *True*, simply add the function to your existing profile here:

C:\Users<USERSNAME>\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

If that command comes back *False*, enter the below command:

New-Item -Path $PROFILE -Type File -force

ONLY DO THIS IF NO PROFILE IS FOUND AS THE -force FLAG WILL OVERWRITE EXISTING PROFILES!!

Once you’ve input this command, go to the path specified above and copy/paste the function into the profile!

2 thoughts on “How to add a touch function to PowerShell

  1. GK

    Hello,
    Not sure if this space is still active but I’m having trouble with a step that is missing here. I have verified that the profile/path exists, however I am unclear on the intermediary of ” simply add the function to your existing profile”. How do you add to it? Do I open the PowerShell_profile.ps1 and simply copy and paste the touch function into that file?

    1. chrisfwilliams Post author

      Hey! Yes open the profile & copypasta into it 🙂

Comments are closed.