I have a directory on my desktop created using PowerShell, and now I'm trying to create a text file within it.
I did change directory to the new one, and typed touch textfile.txt
.
This is the error message I get:
touch : 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:1+ touch file.txt+ ~~~~~+ CategoryInfo : ObjectNotFound: (touch:String) [], CommandNotFoundException+ FullyQualifiedErrorId : CommandNotFoundException`
Why is it not working? Will I have to use Git Bash all the time?
Best Answer
If you need a command touch
in PowerShell you could define a function that does The Right Thing™:
function touch {Param([Parameter(Mandatory=$true)][string]$Path)if (Test-Path -LiteralPath $Path) {(Get-Item -Path $Path).LastWriteTime = Get-Date} else {New-Item -Type File -Path $Path}}
Put the function in your profile so that it's available whenever you launch PowerShell.
Defining touch
as an alias (New-Alias -Name touch -Value New-Item
) won't work here, because New-Item
has a mandatory parameter -Type
and you can't include parameters in PowerShell alias definitions.
If you're using Windows Powershell, the equivalent command to Mac/Unix's touch is: New-Item textfile.txt -type file
.
For single file creation in power shell :ni textfile.txt
For multiple files creation at a same time:touch a.txt,b.html,x.js
is the linux commandni a.txt,b.html,x.js
is the windows power shell command
As Etan Reisner pointed out, touch
is not a command in Windows nor in PowerShell.
If you want to just create a new file quickly (it looks like you're not interested in the use case where you just update the date of an existing file), you can use these:
$null > textfile.txt$null | sc textfile.txt
Note that the first one will default to Unicode, so your file won't be empty; it will contain 2 bytes, the Unicode BOM.
The second one uses sc
(an alias for Set-Content
), which defaults to the system's active ANSI code page when used on the FileSystem, which uses no BOM and therefore truly creates an empty file.
If you use an empty string (''
or ""
or [String]::Empty
) instead of $null
you'll end up with a line break also.
if you are using node, just use this command and it will install touch.
npm install touch-cli -g
Here's the touch
method with better error handling:
function touch{$file = $args[0]if($file -eq $null) {throw "No filename supplied"}if(Test-Path $file){throw "file already exists"}else{# echo $null > $fileNew-Item -ItemType File -Name ($file)}}
Add this function to C:\Program Files\PowerShell\7\Microsoft.PowerShell_profile.ps1
(create this file if doesn't exist).
Use it like this: touch hey.js
cd into the your path where you want the file to be created and then type...
New-item -itemtype file yourfilenamehere.filetype
example
New-item -itemtype file index.js
To generalize Ansgar Wiechers' helpful answer, in order to support:
multiple files as input, passed as individual arguments (
touch file1 file2
, as with the Unixtouch
utility) rather than as an array argument (touch file1, file2
), the latter being the PowerShell-idiomatic way to pass multiple values to a single parameter.wildcard patterns as input (only meaningful when targeting existing files, to update their last-write timestamps).
-Verbose
to get feedback on what actions are performed.
Note:
The function only emulates the core functionality of the Unix
touch
utility:- for existing files, it sets the last-write and last-access timestamps to the current point in time,
- whereas non-existing files are created, with no content.
For a more complete emulation that is also more PowerShell-idiomatic,
Touch-File
, see this answer.The function requires PowerShell version 4 or higher, due to use of the
.ForEach()
array method (though it could easily be adapted to earlier versions).
function touch {<#.SYNOPSISEmulates the Unix touch utility's core functionality.#>param([Parameter(Mandatory, ValueFromRemainingArguments)][string[]] $Path)# For all paths of / resolving to existing files, update the last-write timestamp to now.if ($existingFiles = (Get-ChildItem -File $Path -ErrorAction SilentlyContinue -ErrorVariable errs)) {Write-Verbose "Updating last-write and last-access timestamps to now: $existingFiles"$now = Get-Date$existingFiles.ForEach('LastWriteTime', $now)$existingFiles.ForEach('LastAccessTime', $now)}# For all non-existing paths, create empty files.if ($nonExistingPaths = $errs.TargetObject) {Write-Verbose "Creatng empty file(s): $nonExistingPaths"$null = New-Item $nonExistingPaths}}
The combination of New-Item
with the Name
, ItemType
, and Path
parameters worked for me. In my case, to create a _netrc
file for git:
New-Item -Name _netrc -ItemType File -Path $env:userprofile
References
Permanently authenticating with Git repositories - Atlassian Documentation
Powershell - Windows Environment variables - PowerShell - SS64.com
New-Item - PowerShell - SS64.com
How about just use this to create .txt file in PowerShell.
New-Item .txt
Check this website for more information.
I like to extend the function to create a non-existing folder by default. If you want the traditional behavior providing the option is nice.
function touch {param([string]$Name,[switch]$Traditional)if (-not $Traditional) {$names = $Name.Replace("\", "/").Replace("//", "/") -split "/"$nameNormalized = $names -join "/"$nameNormalized = $nameNormalized.TrimEnd($names[$names.count - 1]).TrimEnd("/")if (-not (Test-Path $nameNormalized) -and ($names.count -gt 1)) { New-Item -Path $nameNormalized -ItemType Directory > $null} }if (-not (Test-Path $Name)) {New-Item -Path $Name -ItemType File >$null}else {(Get-Item -Path $Name).LastWriteTime = Get-Date}}
usage:
# will make the folder if it doesn't existtouch filepath\file.extension# will throw exception if the folder doesn't existtouch filepath\file.extension -Traditional# both will make the file or update the LastWriteTime if the file already exists
If you are using the Window's Power Shell, then the touch command may show an error.Use:
New-Item* filename.filetype
or
ni* filename.filetype
For example:
New-Item name.txt or ni name.txt
In my case, Im just create a alias do command
niin my
user_profile.ps1` file.
Set-Alias touch ni
Then I can use this command to create files, for example: touch example.js anotherone.md another.tsx
Put this in your Windows pwsh profile and reload
function touch ($myNewFileName){New-Item $myNewFileName -type file}
touch seems to work in pwsh OOB in MacOS