Skip to content

Instantly share code, notes, and snippets.

@belsrc
Last active December 6, 2019 00:36
Show Gist options
  • Save belsrc/451921b27eadfa4479e1 to your computer and use it in GitHub Desktop.
Save belsrc/451921b27eadfa4479e1 to your computer and use it in GitHub Desktop.
touch command in Powershell
# Type 'notepad $profile' to open your profile ps1 file and add
# This will add the file if it doesn't exist or update the
# last write time if it does
# If you get some message about not being able to run scripts then you need to type the following command and restart powershell
# Set-ExecutionPolicy Bypass
Function touch {
$file = $args[0]
if($file -eq $null) {
throw "No filename supplied"
}
if(Test-Path $file) {
(Get-ChildItem $file).LastWriteTime = Get-Date
}
else {
echo $null > $file
}
}
@jefflindholm
Copy link

change that echo to
New-Item -force -type file $file
that will create the entire path for the file if it does not exist, and it will be 0 in length as well just remove -force if you don't want auto directory creation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment