Power shell script to clear RDP connection history from registry - powershell

I am new to Powershell but I have stuck in one place that is elaborate in the following details.
I want to perform a PowerShell script in which it will delete the RDP connection history from the registry (MRU number) but it will not delete all history, first, it will check one by one file and ask for approving whether to delete or not then deletion process will start. and after completion of the deletion process, it will restart the server again.

The following syntax will cycle through all 10 possible entries and ask you Y/N to delete them individually. Note that Windows only retains a max of 10 entries. Also note that Windows also retains similar data in "\documents\default.rdp", so this file should be deleted as part of your process.
#echo off
SET count=0
:AGAIN
reg query "HKEY_CURRENT_USER\Software\Microsoft\Terminal Server Client\Default" /s | findstr "MRU%count%"
if %errorlevel% EQU 0 (reg delete "HKEY_CURRENT_USER\Software\Microsoft\Terminal Server Client\Default" /v "MRU%count%") ELSE (goto ExitScript)
set /a count+=1
if %count% GEQ 10 GOTO ExitScript
GOTO AGAIN
:ExitScript

Related

Batch Code to Skip Script if File is too Old

My Problem
As there are a ton of threads that address 'using a batch file to return file modify date' (or delete files older than, etc) - let me first specify my issue.
I'm looking to create a batch (not PowerShell, etc) that will return the last modify date of a specific file given UNC path and filename.
Code, Attempt 1
I've taken a peek at a few potential solutions on other threads, but I've run into a number of unique issues. The first and most obvious solution for this would be the "ForFiles" command in batch. For example:
set myPath=\\myUNCpath
set myFile=myFileName.csv
forfiles /p "%myPath%" /m %myFile% /c "GoTo OldFile" /d -6
Thus, if the file is older than I want -- jump to a specific section of my batch for that. However, this yields the error:
ERROR: UNC paths (\machine\share) are not supported.
However, this cmd won't work due to the use of UNC (which is critical as this batch is called by system's task scheduler). So it seems like the 'ForFiles' cmd is out.
Code, Attempt 2
I could go a more round about way of doing it, but simply retrieving the last modified date of the file (which in batch would return a string). I can truncate the string to the necessary date values, convert to a date, and then compare to current date. To do that, I've also looked into just using a for loop such as:
set myFile=\\myUNCpath\myFileName.csv
echo %myFile%
pause
FOR %%f IN (%myFile%) DO SET myFileDate=%%~tf
echo %myFileDate%
pause
Though my first debug echo provides the proper full file name, my second debug just returns ECHO is off., which tells me it's either not finding the file or the for loop isn't returning the file date. Not sure why.
I've also tried minor changes to this just to double check environmental variable syntax:
FOR %%f IN (%myFile%) DO SET myFileDate=%%~ta
Returns %ta
And finally:
FOR %%f IN (%myFile%) DO SET myFileDate=%~ta
Which without the extra '%', just crashes the batch.
I'm really at a loss at this point. So any tips or guidance would be greatly appreciated!
Using forfiles to a UNC path can be used using PushD
For just echoing the file older than x in UNC path, simply just use
PushD %myPath% &&(
forfiles -s -m %myFile% -d -6 -c "cmd /c echo /q #file
) & PopD
Here is one way how to use goto if file older than x found in UNC path using your examples.
if not exist "C:\temp" mkdir C:\temp
if not exist "C:\temp\test.txt" echo.>"c:\temp\test.txt"
break>"c:\temp\test.txt"
set myPath=\\myUNCpath
set myFile=myFileName.csv
PushD %myPath% &&(
forfiles -s -m %myFile% -d -6 -c "cmd /c echo /q #file >> c:\temp\test.txt"
) & PopD
for /f %%i in ("C:\temp\test.txt") do set size=%%~zi
if %size% gtr 0 goto next
:next
Huge thanks to both #Squashman and #MadsTheMan for the help. Luckily this batch finally is the piece of code that I can take to my boss to push switching over to at least using PowerShell!
Anyway, for those of you looking for the best way to get a UNC path file's modify date, here is what I've come up with. Not very different than other threads (and a thanks goes out to Squashman for spotting my missing " " in the for loop).
set myFile=\\myUNCpath\myFileName.ext
FOR %%f IN ("%myFile%") DO SET myFileDate=%%~tf
::And if you'd like to try to do long winded calculations you can further break the dates down via...
set fileMonth=%myFileDate:~0,2%
set fileDay=%myFileDate:~3,2%
set fileYear=%myFileDate:~6,4%
set currentDate=%date%
set currentMonth=%currentDate:~4,2%
set currentDay=%currentDate:~7,2%
set currentYear=%currentDate:~10,4%
The plan was to then convert the strings to integers, and then use a switch-case block to determine if the variance between dates was acceptable... blah blah blah.
Long story short - if you are limited to batch (and not creating secondary files -- such as a csv or the temp file suggested by MadsTheMan) you're going to have a really long script on your hands that still might have flaws (like leap year, etc unless you're adding even MORE code), when you could just make the comparison calculation using one line of code in other programs.

Pre-Commit Windows Hook to Require Issue Number in Comment

I am looking for a way to reject commits that lack a bug issue number in their comments. The system I am setting up uses VisualSVN Server on Windows Server 2012 and MantisBT on a LAMP server for bug tracking. This question has been asked before, but the repository in that case was on a LAMP server. I am looking for a batch or PowerShell script which does not depend on PHP or compiling code. Ideally, the hook would require "issue #1234" or "issues #12 and #34" to be present somewhere in the commit message.
Based on this batch file:
After this block:
setlocal enabledelayedexpansion
set REPOS=%1
set TXN=%2
set SVNLOOK="%VISUALSVN_SERVER%\bin\svnlook.exe"
REM Concatenate all the lines in the commit message
FOR /F "usebackq delims==" %%g IN (`%SVNLOOK% log -t %TXN% %REPOS%`) DO SET M=!M!%%g
add the check:
echo !M! | findstr /i /r /c:"issues* *#[0-9][0-9]*" >nul
if errorlevel 1 (
echo "Sorry, commit message should reference an issue" >&2
exit /b 1
)

How can i use Relative or Environment variable in Batch command

I am running below command to run Soapui Test suite and it is working fine
testrunner.bat -s"TestSuite4" "D:\Invesco\JP Groovy Code\ExploreGroovy.xml"
I ahve also used with below command and it is working fine as well
testrunner.bat -s"TestSuite4" "%USERPROFILE%\ExploreGroovy.xml"
Now I have added one Envrionment variable 'EnvP' and its value id 'D:\Invesco' and tried with following command but it is not working.
testrunner.bat -s"TestSuite4" "%EnvP%\ExploreGroovy.xml"
Can some one help me in this. I don't want to give hard coded path of any drive. Please suggest if anyone has any other solution.
Thanks.
the process starting testrunner.bat (probably explorer.exe?) must
know about the new variable. have you tried logging out and in again
after setting it?
if it is cmd, try finding the variable with set | find "EnvP". If it is not there, you need to start a new cmd session.
Use these commands and you should see why it fails:
#echo "%EnvP%"
#if not exist "%EnvP%\ExploreGroovy.xml" #echo Ouch!
#pause
testrunner.bat -s"TestSuite4" "%EnvP%\ExploreGroovy.xml"
#pause
For all my SoapUI projects I have multiple .bat scripts in the same location as the project.xml file to run different sets of test suites, and all of it goes into your source repository.
IF NOT DEFINED SOAPUI_ROOT SET SOAPUI_ROOT=%ProgramFiles%\SmartBear\soapUI-Pro-4.6.4
REM make certain we are where we _think_ we are
CD %~dp0
REM cleanup previous results
DEL /f /q *.log*
RMDIR /s /q results
REM run the tests
CALL "%SOAPUI_ROOT%\bin\testrunner.bat" -s"Smoke TestSuite" -fresults My-soapui-project.xml
REM determine if there are failures
IF errorlevel 0 (
ECHO All tests passed.
PAUSE
EXIT 0
) ELSE (
ECHO There are failures!
PAUSE
EXIT 100
)

Batch that runs a timer in the background. How?

I was wondering if theres a way to make a batch file that uses a timer script (such as the TIMEOUT command) and while the timer is running, make the batch file do other processes. But when the timer runs out, then exit. (Or carry out another small process) I know this means that te batch file would have to run multiple processes at once, i just want to know if its possible somehow.
Any ideas?
You can use start /b cmd /c myTimeout.bat
This starts a new application in the same window (start /?)
With this mechanism you should be able do multiple jobs at the same time
EDIT
For a simple timeout this seems to be a bit oversized.
For a timeout of 6 seconds you can use something like
set startTime=%time%
call :timeToMs startMs startTime
set /a timeoutAt=startMs + 6000
:loop
... wait for something, like a file is created, or a CD is inserted
if job_is_ready goto :leaveTheLoop
REM Test if the timeout is reached
set currentTime=%time%
call :timeToMs nowMs currentTime
if nowMs GTR timeoutAt goto :leaveTheLoop
goto :loop
:leaveTheLoop
It fits better for real parallel jobs, like a display job and a key-input job for a game.
modified the code from jeb so that it works:
#echo off
set /A startMs=10*%time:~9,2%+1000*%time:~6,2%+60000*%time:~3,2%+3600000*%time:~0,2%
REM the 6000 in the next line is how many milliseconds of a delay you want
set /a timeoutAt=%startMs%+6000
:loop
REM 'passive' code here:
REM Test if the timeout is reached
2>NUL set /A nowMs=10*%time:~9,2%+1000*%time:~6,2%+60000*%time:~3,2%+3600000*%time:~0,2%
if /I %nowMs% GTR %timeoutAt% goto leaveTheLoop
goto loop
:leaveTheLoop
REM do the things you want to do when the timer ends

How to launch Windows' RegEdit with certain path?

How do I launch Windows' RegEdit with certain path located, like "HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\8.0", so I don't have to do the clicking?
What's the command line argument to do this? Or is there a place to find the explanation of RegEdit's switches?
Use the following batch file (add to filename.bat):
REG ADD HKCU\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit /v LastKey /t REG_SZ /d Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Veritas\NetBackup\CurrentVersion\Config /f
START regedit
to replace:
Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Veritas\NetBackup\CurrentVersion\Config
with your registry path.
There's a program called RegJump, by Mark Russinovich, that does just what you want. It'll launch regedit and move it to the key you want from the command line.
RegJump uses (or at least used to) use the same regedit window on each invoke, so if you want multiple regedit sessions open, you'll still have to do things the old fashioned way for all but the one RegJump has adopted. A minor caveat, but one to keep note of, anyway.
From http://windowsxp.mvps.org/jumpreg.htm (I have not tried any of these):
When you start Regedit, it automatically opens the last key that was viewed. (Registry Editor in Windows XP saves the last viewed registry key in a separate location). If you wish to jump to a particular registry key directly without navigating the paths manually, you may use any of these methods / tools.
Option 1
Using a VBScript: Copy these lines to a Notepad document as save as registry.vbs
'Launches Registry Editor with the chosen branch open automatically
'Author : Ramesh Srinivasan
'Website: http://windowsxp.mvps.org
Set WshShell = CreateObject("WScript.Shell")
Dim MyKey
MyKey = Inputbox("Type the Registry path")
MyKey = "My Computer\" & MyKey
WshShell.RegWrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit\Lastkey",MyKey,"REG_SZ"
WshShell.Run "regedit", 1,True
Set WshShell = Nothing
Double-click Registry.vbs and then type the full registry path which you want to open.
Example: HKEY_CLASSES_ROOT\.MP3
Limitation: The above method does not help if Regedit is already open.
Note: For Windows 7, you need to replace the line MyKey = "My Computer\" & MyKey with MyKey = "Computer\" & MyKey (remove the string My). For a German Windows XP the string "My Computer\" must be replaced by "Arbeitsplatz\".
Option 2
Regjump from Sysinternals.com
This little command-line applet takes a registry path and makes Regedit open to that path. It accepts root keys in standard (e.g. HKEY_LOCAL_MACHINE) and abbreviated form (e.g. HKLM).
Usage: regjump [path]
Example: C:\Regjump HKEY_CLASSES_ROOT\.mp3
Option 3
12Ghosts JumpReg from 12ghosts.com
Jump to registry keys from a tray icon! This is a surprisingly useful tool. You can manage and directly jump to frequently accessed registry keys. Unlimited list size, jump to keys and values, get current key with one click, jump to key in clipboard, jump to same in key in HKCU or HKLM. Manage and sort keys with comments in an easy-to-use tray icon menu. Create shortcuts for registry keys.
I'd also like to note that you can view and edit the registry from within PowerShell. Launch it, and use set-location to open the registry location of your choice. The short name of an HKEY is used like a drive letter in the file system (so to go to HKEY_LOCAL_MACHINE\Software, you'd say: set-location hklm:\Software).
More details about managing the registry in PowerShell can be found by typing get-help Registry at the PowerShell command prompt.
Here is one more batch file solution with several enhancements in comparison to the other batch solutions posted here.
It sets also string value LastKey updated by Regedit itself on every exit to show after start the same key as on last exit.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "RootName=Computer"
set "RegKey=%~1"
if defined RegKey goto PrepareKey
echo/
echo Please enter the path of the registry key to open.
echo/
set "RegKey="
set /P "RegKey=Key path: "
rem Exit batch file without starting Regedit if nothing entered by user.
if not defined RegKey goto EndBatch
:PrepareKey
rem Remove double quotes and square brackets from entered key path.
set "RegKey=%RegKey:"=%"
if not defined RegKey goto EndBatch
set "RegKey=%RegKey:[=%"
if not defined RegKey goto EndBatch
set "RegKey=%RegKey:]=%"
if not defined RegKey goto EndBatch
rem Replace hive name abbreviation by appropriate long name.
set "Abbreviation=%RegKey:~0,4%"
if /I "%Abbreviation%" == "HKCC" set "RegKey=HKEY_CURRENT_CONFIG%RegKey:~4%" & goto GetRootName
if /I "%Abbreviation%" == "HKCR" set "RegKey=HKEY_CLASSES_ROOT%RegKey:~4%" & goto GetRootName
if /I "%Abbreviation%" == "HKCU" set "RegKey=HKEY_CURRENT_USER%RegKey:~4%" & goto GetRootName
if /I "%Abbreviation%" == "HKLM" set "RegKey=HKEY_LOCAL_MACHINE%RegKey:~4%" & goto GetRootName
if /I "%RegKey:~0,3%" == "HKU" set "RegKey=HKEY_USERS%RegKey:~3%"
:GetRootName
rem Try to determine automatically name of registry root.
if not exist %SystemRoot%\Sysnative\reg.exe (set "RegEXE=%SystemRoot%\System32\reg.exe") else set "RegEXE=%SystemRoot%\Sysnative\reg.exe"
for /F "skip=2 tokens=1,2*" %%K in ('%RegEXE% QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit" /v "LastKey"') do if /I "%%K" == "LastKey" for /F "delims=\" %%N in ("%%M") do set "RootName=%%N"
rem Is Regedit already running?
%SystemRoot%\System32\tasklist.exe /NH /FI "IMAGENAME eq regedit.exe" | %SystemRoot%\System32\findstr.exe /B /I /L regedit.exe >nul || goto SetRegPath
echo/
echo Regedit is already running. Path can be set only when Regedit is not running.
echo/
set "UserChoice=N"
set /P "UserChoice=Terminate Regedit (y/N): "
if /I "%UserChoice:"=%" == "y" %SystemRoot%\System32\taskkill.exe /IM regedit.exe >nul 2>nul & goto SetRegPath
echo Switch to running instance of Regedit without setting entered path.
goto StartRegedit
:SetRegPath
rem Add this key as last key to registry for Regedit.
%RegEXE% ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit" /v "LastKey" /d "%RootName%\%RegKey%" /f >nul 2>nul
:StartRegedit
if not exist %SystemRoot%\Sysnative\cmd.exe (start %SystemRoot%\regedit.exe) else %SystemRoot%\Sysnative\cmd.exe /D /C start %SystemRoot%\regedit.exe
:EndBatch
endlocal
The enhancements are:
Registry path can be passed also as command line parameter to the batch script.
Registry path can be entered or pasted with or without surrounding double quotes.
Registry path can be entered or pasted or passed as parameter with or without surrounding square brackets.
Registry path can be entered or pasted or passed as parameter also with an abbreviated hive name (HKCC, HKCU, HKCR, HKLM, HKU).
Batch script checks for already running Regedit as registry key is not shown when starting Regedit while Regedit is already running. The batch user is asked if running instance should be terminated to restart it for showing entered registry path. If the batch user chooses not to terminate all instances of Regedit, Regedit is started without setting entered path resulting (usually) in just getting Regedit window to foreground.
The batch file tries to automatically get name of registry root which is on English Windows XP My Computer, on German Windows XP, Arbeitsplatz, and on Windows 7 and newer Windows just Computer. This could fail if the value LastKey of Regedit is missing or empty in registry. Please set the right root name in third line of the batch code for this case.
The batch file runs on 64-bit Windows always Regedit in 64-bit execution environment even on batch file being processed by 32-bit %SystemRoot%\SysWOW64\cmd.exe on 64-bit Windows which is important for registry keys affected by WOW64.
Copy the below text and save it as a batch file and run
#ECHO OFF
SET /P "showkey=Please enter the path of the registry key: "
REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit" /v "LastKey" /d "%showkey%" /f
start "" regedit
Input the path of the registry key you wish to open when the batch file prompts for it, and press Enter. Regedit opens to the key defined in that value.
I thought this C# solution might help:
By making use of an earlier suggestion, we can trick RegEdit into opening the key we want even though we can't pass the key as a parameter.
In this example, a menu option of "Registry Settings" opens RegEdit to the node for the program that called it.
Program's form:
private void registrySettingsToolStripMenuItem_Click(object sender, EventArgs e)
{
string path = string.Format(#"Computer\HKEY_CURRENT_USER\Software\{0}\{1}\",
Application.CompanyName, Application.ProductName);
MyCommonFunctions.Registry.OpenToKey(path);
}
MyCommonFunctions.Registry
/// <summary>Opens RegEdit to the provided key
/// <para><example>#"Computer\HKEY_CURRENT_USER\Software\MyCompanyName\MyProgramName\"</example></para>
/// </summary>
/// <param name="FullKeyPath"></param>
public static void OpenToKey(string FullKeyPath)
{
RegistryKey rKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(#"SOFTWARE\Microsoft\Windows\CurrentVersion\Applets\Regedit", true);
rKey.SetValue("LastKey",FullKeyPath);
Process.Start("regedit.exe");
}
Of course, you could put it all in one method of the form, but I like reusablity.
Here is a simple PowerShell function based off of this answer above https://stackoverflow.com/a/12516008/1179573
function jumpReg ($registryPath)
{
New-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit" `
-Name "LastKey" `
-Value $registryPath `
-PropertyType String `
-Force
regedit
}
jumpReg ("Computer\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run") | Out-Null
The answer above doesn't actually explain very well what it does. When you close RegEdit, it saves your last known position in HKCU:\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit, so this merely replaces the last known position with where you want to jump, then opens it.
Create a BAT file using clipboard.exe and regjump.exe
to jump to the key in the clipboard:
clipboard.exe > "%~dp0clipdata.txt"
set /p clipdata=input < "%~dp0clipdata.txt"
regjump.exe %clipdata%
( %~dp0 means "the path to the BAT file" )
Building on lionkingrafiki's answer, here's a more robust solution that will accept a reg key path as an argument and will automatically translate HKLM to HKEY_LOCAL_MACHINE or similar as needed. If no argument, the script checks the clipboard using the htmlfile COM object invoked by a JScript hybrid chimera. The copied data will be split and tokenized, so it doesn't matter if it's not trimmed or even among an entire paragraph of copied dirt. And finally, the key's existence is verified before LastKey is modified. Key paths containing spaces must be within double quotes.
#if (#CodeSection == #Batch) #then
:: regjump.bat
#echo off & setlocal & goto main
:usage
echo Usage:
echo * %~nx0 regkey
echo * %~nx0 with no args will search the clipboard for a reg key
goto :EOF
:main
rem // ensure variables are unset
for %%I in (hive query regpath) do set "%%I="
rem // if argument, try navigating to argument. Else find key in clipboard.
if not "%~1"=="" (set "query=%~1") else (
for /f "delims=" %%I in ('cscript /nologo /e:JScript "%~f0"') do (
set "query=%%~I"
)
)
if not defined query (
echo No registry key was found in the clipboard.
goto usage
)
rem // convert HKLM to HKEY_LOCAL_MACHINE, etc. while checking key exists
for /f "delims=\" %%I in ('reg query "%query%" 2^>NUL') do (
set "hive=%%~I" & goto next
)
:next
if not defined hive (
echo %query% not found in the registry
goto usage
)
rem // normalize query, expanding HKLM, HKCU, etc.
for /f "tokens=1* delims=\" %%I in ("%query%") do set "regpath=%hive%\%%~J"
if "%regpath:~-1%"=="\" set "regpath=%regpath:~0,-1%"
rem // https://stackoverflow.com/a/22697203/1683264
>NUL 2>NUL (
REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit"^
/v "LastKey" /d "%regpath%" /f
)
echo %regpath%
start "" regedit
goto :EOF
#end // begin JScript hybrid chimera
// https://stackoverflow.com/a/15747067/1683264
var clip = WSH.CreateObject('htmlfile').parentWindow.clipboardData.getData('text');
clip.replace(/"[^"]+"|\S+/g, function($0) {
if (/^\"?(HK[CLU]|HKEY_)/i.test($0)) {
WSH.Echo($0);
WSH.Quit(0);
}
});
This seems horribly out of date, but Registration Info Editor (REGEDIT) Command-Line Switches claims that it doesn't support this.
You can make it appear like regedit does this behaviour by creating a batch file (from the submissions already given) but call it regedit.bat and put it in the C:\WINDOWS\system32 folder. (you may want it to skip editting the lastkey in the registry if no command line args are given, so "regedit" on its own works as regedit always did) Then "regedit HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\8.0" will do what you want.
This uses the fact that the order in PATH is usually C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem; etc
If the main goal is just to avoid "the clicking", then in Windows 10 you can just type or paste the destination path into RegEdit's address bar and hit enter.
The Computer\ prefix here is added automatically. It will also work if you simply type or paste a path starting with e.g. HKEY_CURRENT_USER\....
PowerShell code:
# key you want to open
$regKey = "Computer\HKEY_LOCAL_MACHINE\Software\Microsoft\IntuneManagementExtension\Policies\"
# set starting location for regedit
Set-ItemProperty "HKCU:\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit" "LastKey" $regKey
# open regedit (-m allows multiple regedit windows)
regedit.exe -m
This is the best answer overall, as it's quick, simple and there's no need to install any program.
By Byron Persino, improved by Matt Miller. (Many thanks to both of them!)
I'm rewording more correctly and clearly to help other readers like me, as I had a lot of trouble getting it clear and make it working.
Make a .bat file, eg. 'GoToRegEditPath.bat' , write the following code inside and save it:
CODE:
#echo off
set /p regPath="Open regedit at path: "
REG ADD HKCU\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit /v LastKey /t REG_SZ /d "%regPath%" /f
START regedit
exit
:: source:
https://stackoverflow.com/questions/137182/how-to-launch-windows-regedit-with-certain-path?answertab=modifieddesc#tab-top
Maybe this .bat use must "Run as Administrator"
To use it, Just run it and paste (R-Click) in it the copied RegEdit Path.
Tip: if R-click does not work inside command prompt:
R-click on title bar > Properties > check both under "Edit Options"