locked
powershell touch:string [] RRS feed

  • Question

  • I created two empty files using powershell yesterday using the touch command. Today, I get an error that there is no touch command. The error includes the text in the title of this thread so it must know, somewhere inside, about a touch command - or did it imply that syntax from me trying to call touch with: touch myfile1

    I see a lot of scripts for creating a touch command but I know I did this yesterday and can't today.  I was pleasantly surprised when I tried touch and found that Microsoft had finally added it.... or I fell on my head and got knocked senseless and don't remember.... It's got to be one or the other.

    Tuesday, March 13, 2018 8:41 PM

Answers

All replies

  • "touch" is not a PowerShell command.

    "touch" is a native unix command.  On Windows you must install the command or copy it from somewhere.

    Here is a PowerShell equivalent script. https://ss64.com/ps/syntax-touch.html


    \_(ツ)_/

    Tuesday, March 13, 2018 8:49 PM
  • How about 


    new-item



    • Edited by JS2010 Tuesday, March 13, 2018 8:54 PM
    Tuesday, March 13, 2018 8:54 PM
  • New-Item is not "touch"  It creates a new file with current timestamp.  If you force it on an existing file it just deletes and creates a new file.  "touch" changes the time on an already existing file.


    \_(ツ)_/

    Tuesday, March 13, 2018 8:57 PM
  • Here is a quick and dirty "touch" method:

    (Get-Item test.txt).CreationTime = [datetime]::Now


    \_(ツ)_/

    Tuesday, March 13, 2018 9:01 PM
  • I figured out some of it.  At work, it didn't work. At home, it did. I didn't look in my profile to verify yet but I'm guessing that, in time beyond memory, I had added a touch script to my profile.
    Wednesday, March 14, 2018 12:56 PM
  • Elsewhere, someone had recommending using:

    echo $null >> mynewfile.txt
    That didn't work because it created a 2-byte file.  Whatever was in the file was non-printable on the screen and I didn't bother to look deeper because I needed a zero-byte.  I guess I should have grabbed the script I had at home and put it here for future reference.  I'll try to remember later.

    Wednesday, March 14, 2018 1:00 PM
  • How to create an empty file in PowerShell:

    New-Item empty.txt -ItemType File

    This has nothing to do with "touch" which is a utility that changes the dates on files.


    \_(ツ)_/

    Wednesday, March 14, 2018 1:06 PM