Return Keystroke in PowerShell

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:

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

 

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!!!

<# 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

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

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.

 

 

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

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

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

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?

I couldn’t understand

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

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

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

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?

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: (:slight_smile: [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.

 

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

Sorry, that was a typo. its

System.Windows.Forms

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

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?

 

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/

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?

Thanks :slight_smile:

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

https://codingbee.net/powershell/powershell-parameterizing-your-powershell-scripts.

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?

 

variables wont get expanded when wrapped in single quotes.

#wrong
‘$old’,’$new’ 

#correct
$old,$new
#or
"$old","$new"

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!!!

I’m sure google has more information than any of us :slight_smile:

https://social.technet.microsoft.com/Forums/scriptcenter/en-US/286b0626-d1ea-48d3-91ab-9129a258e40a/how-can-i-replace-text-in-xml-node-with-powershell?forum=ITCG

https://jdhitsolutions.com/blog/scripting/3727/find-and-replace-text-with-powershell/

Shukriya.

 

Very helpful information.

 

Have a blessed day my friend!!!