none
Right click copy from shell RRS feed

  • Question

  • Similar to this article,

    https://social.technet.microsoft.com/Forums/en-US/022b57db-02d7-4446-8542-2dc4052f4a9b/windows-context-menu-item-rename-file-with-random-hash-pipeline-expression-error?forum=winserverpowershell

    I want to create a right click Backup File shell option.

    I have this thus far,

    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoExit -NoProfile -NonInteractive -Command "Get-ChildItem %1 | ForEach-Object {Copy-Item -path $_.fullname -destination ($_.DirectoryName + '\' + $_.Basename + '_' + @(Get-Date -uformat "%Y%m%d-%H%M%S") + $_.Extension)}"

    I can run the command in raw Powershell console by changing the %1 to a hard path\file and it works fine.

    When I add it to a shell command in HKCR\*\shell\<my command> Powershell complains that my path is wrong on the copy-item

    This is the error I get:

    Copy-Item : The given path's format is not supported.
    At line:1 char:68
    + ... ach-Object {Copy-Item -path $_.fullname -destination ($_.DirectoryNam ...
    +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [Copy-Item], NotSupportedException
        + FullyQualifiedErrorId : System.NotSupportedException,Microsoft.PowerShell.Commands.CopyItemCommand

    An example of what it does is copies a file named backup.reg and calls it backup_20190418-162708.reg

    Convenient if you want to make a copy of a file and add the date and time to the copy.

    Does the shell component get upset with the use of %, ", $ or @?

    Friday, April 19, 2019 2:09 AM

Answers

  • System didn't like the % in the date format, had to double them.

    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -NonInteractive -Command "$a='%1';$a;Get-ChildItem $a|ForEach-Object{Copy-Item -path $_.fullname -destination ($_.DirectoryName + '\' + $_.Basename + '_' + (Get-Date -uformat '%%Y%%m%%d-%%H%%M%%S') + $_.Extension)}"


    • Marked as answer by jbruns Saturday, April 20, 2019 5:18 PM
    • Edited by jbruns Saturday, April 20, 2019 5:21 PM
    Friday, April 19, 2019 5:01 PM

All replies

  • "%1" is not valid in a PowerShell script. That only works as a command line argument.

    Replace %1 with $args[0]


    \_(ツ)_/


    Friday, April 19, 2019 3:08 AM
    Moderator
  • You can also try it like this:

    powershell.exe -NoExit -NoProfile -NonInteractive -Command "$a='%1';Get-ChildItem $a|ForEach-Object{Copy-Item -path $_.fullname -destination ($_.DirectoryName + '\' + $_.Basename + '_' + (Get-Date -uformat '%Y%m%d-%H%M%S') + $_.Extension)}"


    \_(ツ)_/


    Friday, April 19, 2019 3:18 AM
    Moderator
  • Tried your answer and got:

    Copy-Item : The given path's format is not supported.
    At line:1 char:77
    + ... ach-Object {Copy-Item -path $_.fullname -destination ($_.DirectoryNam ...
    +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [Copy-Item], NotSupportedException
        + FullyQualifiedErrorId : System.NotSupportedException,Microsoft.PowerShell.Commands.CopyItemCommand


    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoExit -NoProfile -NonInteractive -Command "$a='%1'; Get-ChildItem $a | ForEach-Object {Copy-Item -path $_.fullname -destination ($_.DirectoryName + '\' + $_.Basename + '_' + @(Get-Date -uformat "%Y%m%d-%H%M%S") + $_.Extension)}"

    Just so you all know, if I use
    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoExit -NoProfile -NonInteractive -Command "Get-ChildItem %1 | Fl *"
    that works fine, so I probably don't need to assign $a to %1



    • Edited by jbruns Friday, April 19, 2019 1:02 PM
    Friday, April 19, 2019 12:34 PM
  • You need to fix it.  The code you posted was badly damaged and is missing a space.  I fixed mine.


    \_(ツ)_/

    Friday, April 19, 2019 1:15 PM
    Moderator
  • System didn't like the % in the date format, had to double them.

    C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -NonInteractive -Command "$a='%1';$a;Get-ChildItem $a|ForEach-Object{Copy-Item -path $_.fullname -destination ($_.DirectoryName + '\' + $_.Basename + '_' + (Get-Date -uformat '%%Y%%m%%d-%%H%%M%%S') + $_.Extension)}"


    • Marked as answer by jbruns Saturday, April 20, 2019 5:18 PM
    • Edited by jbruns Saturday, April 20, 2019 5:21 PM
    Friday, April 19, 2019 5:01 PM