Return Keystroke in PowerShell

Welcome Forums General PowerShell Q&A Return Keystroke in PowerShell

Viewing 18 reply threads
  • Author
    Posts
    • #261305
      Participant
      Topics: 13
      Replies: 40
      Points: 178
      Rank: Participant

      Having a real strange issue here everyone.   I want to include a <RETURN> function at the end of line get-filehash $New -Algorithm MD5   (code shown in its entirety below).

      Funny thing is:  when I run the script it does exactly what I would expect.   It converts the file name to whatever I choose it to be in the User Input section…. but when the script continues to the MD5 Hash line…. it indeed DOES apply an MD5 Hash to the newly named file but the strange part is after doing everything I expect it to do …. it THEN displays the following:

       

      This line is the last one on the script.   It carries out the <Enter Key> keystroke function, then displays the above mentioned code which should have already completed… and it did…. and then when I hit enter again it throws an error.  I love that it carries out the MD5 Hash but dont like seeing that same code appear again when it already completed the task.   Any help to resolve would be great.   I just want it to carry out the function as it does but then stop the program with no further display of the above mentioned.   Here is the code.  Please help!!!

      • This topic was modified 1 month, 4 weeks ago by kvprasoon. Reason: code formatting
      • This topic was modified 1 month, 4 weeks ago by kvprasoon. Reason: formatting
      • This topic was modified 1 month, 4 weeks ago by kvprasoon.
    • #261362
      Senior Moderator
      Topics: 9
      Replies: 1422
      Points: 5,290
      Helping Hand
      Rank: Community MVP

      THEN displays the following

      is it showing the code in the console output? To which window you are sending the keystroke ?

    • #261419
      Participant
      Topics: 13
      Replies: 40
      Points: 178
      Rank: Participant

      KVPRASOON or anyone else out there:

      Yes the code is showing up in the PowerShell Console Output.

      I am sending the code to the PowerShell Window only.

       

      To clarify here is the original code:

      $Current = Read-Host “Please Enter Current File Name”

      $New = Read-Host “Please Enter New File Name”

      Rename-Item -Path “$Current” -NewName “$New”

      <# MD5 Hash Apply To New File Name #>

      get-filehash $New -Algorithm MD5

      Sleep 1

      Add-Type -AssemblyName PresentationFramework[System.Windows.Forms.SendKeys]::SendWait(“{ENTER}”) SendKeys.Send(“{ENTER}”)

       

      All I want is to get a {RETURN} keystroke to occur after the get-filehash $New -Algorithm MD5 line in the code and then the program to terminate.

       

       

    • #261458
      Senior Moderator
      Topics: 9
      Replies: 1422
      Points: 5,290
      Helping Hand
      Rank: Community MVP

      Add-Type -AssemblyName PresentationFramework[System.Windows.Forms.SendKeys]::SendWait(“{ENTER}”) SendKeys.Send(“{ENTER}”)

      Looks like all the code is in a single line, are you sure on this code ? Isn’t it like

      what is SendKeys in SendKeys.Send(), is it a variable?

    • #261470
      Participant
      Topics: 13
      Replies: 40
      Points: 178
      Rank: Participant

      kvprasoon:

      Given the following which is my original code:

      <# Filename Change #>

      $Current = Read-Host “Please Enter Current File Name”

      $New = Read-Host “Please Enter New File Name”

      Rename-Item -Path “$Current” -NewName “$New”

      <# MD5 Hash Apply To New File Name #>

      get-filehash $New -Algorithm MD5

      Sleep 1

      Add-Type -AssemblyName PresentationFramework[System.Windows.Forms.SendKeys]::SendWait(“{ENTER}”) SendKeys.Send(“{ENTER}”) -ErrorAction SilentlyContinue, Stop

      Can you please email me back with how you would re write it to solve this issue please?

      The entire code with your modification please?

    • #261497
      Senior Moderator
      Topics: 9
      Replies: 1422
      Points: 5,290
      Helping Hand
      Rank: Community MVP

      I couldn’t understand

      Above line is syntactically wrong. The correct syntax would be like below. And to use System.Windows.Forms.SendKeys, you should load System.Windows.Forms

      • This reply was modified 1 month, 4 weeks ago by kvprasoon. Reason: Post correction
    • #261503
      Participant
      Topics: 13
      Replies: 40
      Points: 178
      Rank: Participant

      So I say again, given my current coding of :

      <# Filename Change #>

      $Current = Read-Host “Please Enter Current File Name”

      $New = Read-Host “Please Enter New File Name”

      Rename-Item -Path “$Current” -NewName “$New”

      <# MD5 Hash Apply To New File Name #>

      get-filehash $New -Algorithm MD5

      Sleep 1

      Add-Type -AssemblyName PresentationFramework[System.Windows.Forms.SendKeys]::SendWait(“{ENTER}”) SendKeys.Send(“{ENTER}”) -ErrorAction SilentlyContinue, Stop

      Where would you place your suggested correction in my code to solve this problem.

      All I want to do is have a return keystroke in code after -Algorithm MD5

      Have the program execute and then terminate.

       

      Would you, again, be so kind as to take the above code and show me WHERE in it to place any corrections you might have?

    • #261512
      Participant
      Topics: 13
      Replies: 40
      Points: 178
      Rank: Participant

      I run your code and have placed it at the bottom of my script as follows:

      <# Filename Change #>

      $Current = Read-Host “Please Enter Current File Name”

      $New = Read-Host “Please Enter New File Name”

      Rename-Item -Path “$Current” -NewName “$New”

      <# MD5 Hash Apply To New File Name #>

      get-filehash $New -Algorithm MD5

      Sleep 1

      Add-Type -AssemblyName System.Windows.Form
      [System.Windows.Forms.SendKeys]::SendWait(“{ENTER}”)

       

      When I run it, once again the file name change and MD5 Hash take place but again, but now I get the following error message:

      Add-Type : Cannot add type. The assembly ‘System.Windows.Form’ could not be found.
      At line:1 char:1
      + Add-Type -AssemblyName System.Windows.Form
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo : ObjectNotFound: (System.Windows.Form:String) [Add-Type], Exception
      + FullyQualifiedErrorId : ASSEMBLY_NOT_FOUND,Microsoft.PowerShell.Commands.AddTypeCommand

      Add-Type : Cannot add type. One or more required assemblies are missing.
      At line:1 char:1
      + Add-Type -AssemblyName System.Windows.Form
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo : InvalidData: (:) [Add-Type], InvalidOperationException
      + FullyQualifiedErrorId : ASSEMBLY_LOAD_ERRORS,Microsoft.PowerShell.Commands.AddTypeCommand

       

      Then I get a line that reads:  [System.Windows.Forms.SendKeys]::SendWait(“{ENTER}”)

      Upon hitting enter I get the following error message being thrown:

      Unable to find type [System.Windows.Forms.SendKeys].
      At line:1 char:1
      + [System.Windows.Forms.SendKeys]::SendWait(“{ENTER}”)
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo : InvalidOperation: (System.Windows.Forms.SendKeys:TypeName) [], RuntimeException
      + FullyQualifiedErrorId : TypeNotFound

       

      This is such a major problem for what should be something simple which is to get a return keystroke and program termination to occur after the line:

      get-filehash $New -Algorithm MD5

       

      Please advise and thank you for your assistance thus far.

       

    • #261524
      Senior Moderator
      Topics: 9
      Replies: 1422
      Points: 5,290
      Helping Hand
      Rank: Community MVP

      Just asking this question to correct my understanding. How did you end up in this code ?

    • #261527
      Senior Moderator
      Topics: 9
      Replies: 1422
      Points: 5,290
      Helping Hand
      Rank: Community MVP

      Sorry, that was a typo. its

      An see here on how to format code in the forums: https://powershell.org/forums/topic/guide-to-posting-code-2/

      • This reply was modified 1 month, 4 weeks ago by kvprasoon. Reason: post correction
    • #261551
      Participant
      Topics: 13
      Replies: 40
      Points: 178
      Rank: Participant

      Ok that was great info on the Form vs Forms typo.

       

      Now the code runs perfectly but with one exception.

      After the code runs and the file name is changed and MD5 algorithm is applied the following three lines still show up on the PowerShell Prompt:

      Sleep 1

      Add-Type -AssemblyName System.Windows.Forms
      [System.Windows.Forms.SendKeys]::SendWait(“{ENTER}”)

      When I physically hit the <ENTER> key then the following three lines go away with only an empty prompt remaining.

       

      So FINALLY, how do I get those three lines of code to execute, which I believe they already did since I get the desired result of a file name change and the MD5  Hash occuring, and not display on my PowerShell Console Display.  Is there a way to have Powershell ignore displaying those three lines and just terminate the program?

       

    • #261557
      Senior Moderator
      Topics: 9
      Replies: 1422
      Points: 5,290
      Helping Hand
      Rank: Community MVP

      How are you executing ? are these code saved to a file ?
      If you are just copy pasting , then the code will get printed in console to execute.

      Once again, its better to understand the code when posted using code formatting rules defined here: https://powershell.org/forums/topic/guide-to-posting-code-2/

      • This reply was modified 1 month, 4 weeks ago by kvprasoon. Reason: post cvorrection
    • #261569
      Participant
      Topics: 13
      Replies: 40
      Points: 178
      Rank: Participant

      The code works perfectly.

      Thank you so much.   Now for the tricky part.

       

      If I wish to change multiple files with user input how would I go about doing that?  FYI, the File Extensions wont change… just the file names.

      To reiterate this is what the code looks like for the current file name changing function and AGAIN Thank you SOOOO MUCH FOR GETTING ME THIS FAR MY FRIEND!!!!

      $Current = Read-Host “Please Enter Current File Name”

      $New = Read-Host “Please Enter New File Name”

      Rename-Item -Path “$Current” -NewName “$New”

       

      Any code examples you can share with me to make this script handle multiple file changes with user input?

    • #261572
      Senior Moderator
      Topics: 9
      Replies: 1422
      Points: 5,290
      Helping Hand
      Rank: Community MVP

      Thanks 🙂

      You have toe make it as a script with Parameters. Refer blow link as an example.

      PowerShell – Parameterizing your PowerShell scripts

    • #261587
      Participant
      Topics: 13
      Replies: 40
      Points: 178
      Rank: Participant

      Thank you kvprasoon

      Very good information.

      To do a bulk change of file name extensions in one folder I am using the following code:

      <# Multiple Filename Changes #>
      $old = “*.itt”
      $new = “*.xml”

      Get-ChildItem -Filter “*$old*” -Recurse | Rename-Item -NewName {$_.name -replace ‘$old’,’$new’ }

      <# MD5 Hash Apply To New File Name #>

      Sleep 1

      Add-Type -AssemblyName System.Windows.Forms
      [System.Windows.Forms.SendKeys]::SendWait(“{ENTER}”)

      get-filehash $new -Algorithm MD5

      Problem:  While the code runs without throwing any errors, it does NOT change the file extensions in the target directory.  In this case I am using C:\Temp to do my testing.  Any

      Idea why the file extensions are not changing?

       

    • #261689
      Senior Moderator
      Topics: 9
      Replies: 1422
      Points: 5,290
      Helping Hand
      Rank: Community MVP

      variables wont get expanded when wrapped in single quotes.

    • #261761
      Participant
      Topics: 13
      Replies: 40
      Points: 178
      Rank: Participant

      I wanted to take a moment to thank you kvprasoon for all your help.   I got everything working thanks to your wonderful advice.

      You are a blessing to me Sir.

      One last question:

      Do you have any code samples of how to go into a XML file and modify specific elements of it?   It would be like using the Find and Replace function in windows GUI.  Please advise at your convenience and have a very blessed day Sir.  Shukriya!!!

    • #261767
      Senior Moderator
      Topics: 9
      Replies: 1422
      Points: 5,290
      Helping Hand
      Rank: Community MVP
    • #261791
      Participant
      Topics: 13
      Replies: 40
      Points: 178
      Rank: Participant

      Shukriya.

       

      Very helpful information.

       

      Have a blessed day my friend!!!

Viewing 18 reply threads
  • The topic ‘Return Keystroke in PowerShell’ is closed to new replies.