How to add a touch function to PowerShell

By | 2019-05-07

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!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.