Creating new file with touch command in PowerShell error message

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.

Leave a Comment

deneme bonusu veren sitelerbahis casino