Send Keystrokes from Clipboard with Powershell

From WickyWiki
Revision as of 08:31, 31 August 2022 by Wilbert (talk | contribs) (Created page with "Category:Windows Powershell Category:202208 Send Keystrokes from Clipboard with Powershell SendKeys documentation * https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.sendkeys.send?view=windowsdesktop-6.0 Special characters: percent% tilde~ plus+ circumflex^ [brackets] (brackets) 'quotes' `backquotes` "quotes" {accolades} To start this script with a batch file: <source lang=dos> @echo off cd /d C:\Users\wilbert\Documents\Wil...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Send Keystrokes from Clipboard with Powershell

SendKeys documentation

Special characters:

 percent%
 tilde~
 plus+
 circumflex^
 [brackets]
 (brackets)
 'quotes'
 `backquotes`
 "quotes"
 {accolades}

To start this script with a batch file:

@echo off
cd /d C:\Users\wilbert\Documents\Wilbert_Opslag\MsWindows_clean_n_tweak
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noLogo -ExecutionPolicy unrestricted -file "SendClipboardAsKeystrokes.ps1"
if errorlevel 1 pause

Powershell script:


function FuncMain {
	Param($sClipboard)

	$WaitSeconds = 2

	add-type -AssemblyName System.Windows.Forms

	Write-Host ""
	Write-Host "Send clipboard to selected window"
	Write-Host ""

	$sClipboard2 = @()
	$nChars = 0

	Foreach ($s in $sClipboard)
	{
		$nChars = $nChars + $s.Length

		$s2 = $s		
		#escape curly brackets, using curly brackets
		$unique1 = -join( "[",([guid]::NewGuid()).ToString(),"]")
		$unique2 = -join( "[",([guid]::NewGuid()).ToString(),"]")
		$s2 = $s2.replace("{",$unique1)
		$s2 = $s2.replace("}",$unique2)
		$s2 = $s2.replace($unique1,"{{}")
		$s2 = $s2.replace($unique2,"{}}")
		#escape other brackets
		$s2 = $s2.replace("(","{(}")
		$s2 = $s2.replace(")","{)}")
		$s2 = $s2.replace("[","{[}")
		$s2 = $s2.replace("]","{]}")
		#escape other characters: SHIFT=+ ALT=% CTRL=^ ENTER=~
		$s2 = $s2.replace("+","{+}")
		$s2 = $s2.replace("%","{%}")
		#add ESCAPE key to prevent 'accented' characters with certain language settings
		#this will probably frustrate password entry if you use these
		$s2 = $s2.replace("^","{^}{ESCAPE}")
		$s2 = $s2.replace("~","{~}{ESCAPE}")
		$s2 = $s2.replace("'","'{ESCAPE}")
		$s2 = $s2.replace("``","``{ESCAPE}")
		$s2 = $s2.replace("""","""{ESCAPE}")

		$sClipboard2 += $s2
	}

	Write-Host "The clipboard contains" $nChars "characters in" $sClipboard2.Length "line(s)"
	Write-Host "Select window - WITHIN" $WaitSeconds "SECONDS"

	#while( $WaitSeconds -gt 0){
	#	Write-Host $WaitSeconds
	#	$WaitSeconds--
	#	Start-Sleep -Seconds 1
	#}
	Start-Sleep -Seconds $WaitSeconds

	Try {
		Foreach ($s in $sClipboard2)
		{
			#DEBUG: Write-Host $s
			[System.Windows.Forms.SendKeys]::SendWait( $s )
			#press ENTER
			[System.Windows.Forms.SendKeys]::SendWait( '{ENTER}' )
			#next line (if any): undo auto-indent for certain text editors			
			[System.Windows.Forms.SendKeys]::SendWait( '+({TAB}{TAB}{TAB}{TAB}{TAB})' )
		}
	}
	Catch {
		Write-Warning $Error[0]
		Write-Host "Press ENTER to continue"
		Read-Host
	}
}


$sLangMode = $ExecutionContext.SessionState.LanguageMode
If ($sLangMode -ne "FullLanguage") {
	Write-Host ""
	Write-Host "Unable to run script Powershell Using Wrong Language Mode !!"
	Write-Host ""
}
Else {
	$sClipboard = Get-Clipboard
	if ( $sClipboard.Length -eq 0  ) {
	  Write-Host ""
	  Write-Host "The clipboard is empty"
	  Start-Sleep -Seconds 2
	}
	else {
	  FuncMain -sClipboard $sClipboard

	  #[System.Windows.Forms.Clipboard]::Clear()
	  #Write-Host "Clipboard cleared"

	  Start-Sleep -Seconds 2
	}
}