Archive for the ‘Batch-Files’ Category
Posted by jpluimers on 2019/03/13
Based on [WayBack] windows – How to run batch file command with elevated permissions? – Super User:
powershell -command "Start-Process cmd.exe -Verb runas"
This works better than "runas /user:administrator cmd.exe" as that forces to use the specific Administrator account, whereas the PowerShell way allows you to specify the actual account during elevation.
You can extend this to run a command with one or more parameters based on [WayBack] Launch Elevated CMD.exe from Powershell – Stack Overflow (thanks [WayBack] mklement0):
powershell -command "Start-Process cmd.exe -Verb runas -Args /k, call, goto-bin"
This will actually pass “call goto-bin” to cmd.exe which tries to execute the “goto-bin” command (which I have around on the PATH as goto-bin.bat).
You can either use comma-separated parameters or a quoted string. In this expansion, comma-separated is easier in this PowerShell construct.
–jeroen
Posted in Batch-Files, CommandLine, Console (command prompt window), Development, Power User, PowerShell, PowerShell, Scripting, Software Development, Windows | Leave a Comment »
Posted by jpluimers on 2019/02/27
From [WayBack] How do I pass this common property to MSBuild using TeamCity? – Stack Overflow, I learned you can pass properties to msbuild using the /p:propertyname=value or /property:propertyname=value syntax (where you can quote "value" when needed):
I am using the TeamCity Visual Studio runner. I want to add a setting that is not accessible from Visual Studio./Property:FileAlignment=4096I typed that directly into the build step “Command line
However, when passing these parameters to batch files first, be aware that they can strip equals signs from parameters: [WayBack] windows – Preserving “=” (equal) characters in batch file parameters – Stack Overflow
I bumped into this when passing properties to https://bitbucket.org/jeroenp/wiert.me/src/tip/Run-Dependend-rsvars-From-Path.bat
–jeroen
Posted in Batch-Files, Continuous Integration, Development, msbuild, Scripting, Software Development | Leave a Comment »
Posted by jpluimers on 2019/02/15
Up until Widows 8.1, you could use wuapp to start the Windows Update panel.
For a while, Windows 10 needed a cumbersome language specific workaround described at Windows 10 – language neutral batch file to start Windows.
That stopped working after a few builds, but I forgot to make a note in which build exactly. Already in Windows 10 build 10122, the icon in wucltux.dll, so this might have been shortly after the initial “RTM” (retroactively named 1507).
So for a while, I had this batch file:
Since then I had to maintain too many locales running Windows 10. So here is the batch file:
for /f "delims=" %%A in ('PowerShell -Command "(Get-Culture).Name"') do explorer "%LocalAppData%\Packages\windows.immersivecontrolpanel_cw5n1h2txyewy\LocalState\Indexed\Settings\%%A\AAA_SystemSettings_MusUpdate_UpdateActionButton.settingcontent-ms"
It uses these tricks:
- Set output of a command as a variable (in this case a for loop variable)
- Execute PowerShell script in a .bat file
- PowerShell Get-Culture (which gets a .NET CultureInfo instance)
- CultureInfo.Name property (which has the nl-NL, en-US, etc codes in it)
But now I have extended it to support old and new Windows versions:
if exist %windir%\System32\wuapp.exe (
%windir%\System32\rundll32.exe url.dll,FileProtocolHandler wuapp.exe
) else (
%windir%\explorer ms-settings:windowsupdate
)
–jeroen
via: Windows Update Shortcut – Create in Windows 10 – Windows 10 Forums
Posted in Batch-Files, Development, Power User, Scripting, Software Development, Windows | Leave a Comment »
Posted by jpluimers on 2019/01/09
In my search for starting the Windows Credential Manager from the console, I found [WayBack] Credential Manager Shortcut – Create – Windows 7 Help Forums explaining:
%windir%\explorer.exe shell:::{1206F5F1-0569-412C-8FEC-3204630DFB70}
This reminded me of From batch file or shortcut: start Windows Update (via: Windows 7 Help Forums) and batch-file trick: Starting Windows Explorer and selecting a file (“explorer” commandline parameters “/n” “/e” “/select” “/root” “/start” site:microsoft.com).
The odd thing is that some of the GUID shortcuts works fine using the shell::: syntax, but fail with the /e:: syntax, for instance Windows Update until Windows 8.1:
%windir%\explorer.exe shell:::{36eef7db-88ad-4e81-ad49-0e313f0c35f8}
%windir%\explorer.exe /e,::{36eef7db-88ad-4e81-ad49-0e313f0c35f8}
One day I’ll create a table of permutations for various Windows versions and execute options.
For now these links need to suffice:
–jeroen
Posted in Batch-Files, Development, Power User, Scripting, Software Development, Windows, Windows 10, Windows 7, Windows 8, Windows 8.1, Windows 9 | 1 Comment »
Posted by jpluimers on 2018/11/23
I needed a “get only the first result” of WHERE (which is present after Windows 2000, so XP, Server 2003 and up), so based on [WayBack] A 90-byte “whereis” program – The Old New Thing I came up with this:
@echo off
:: based on https://blogs.msdn.microsoft.com/oldnewthing/20050120-00/?p=36653
::for %%f in (%1) do @echo.%%~$PATH:f
for %%e in (%PATHEXT%) do @for %%i in (%1 %~n1%%e) do (
@if NOT "%%~$PATH:i"=="" (
echo %%~$PATH:i
goto :eof
)
)
:: note: WHERE lists all occurrences of a file on the PATH in PATH order
goto :eof
Two changes:
- it takes into account the extension if you specify it (unlike WHERE.EXE)
- it bails out at the first match (like WHERE.EXE)
References:
–jeroen
Posted in Batch-Files, Development, Power User, Scripting, Software Development, The Old New Thing, Windows, Windows Development | Leave a Comment »
Posted by jpluimers on 2018/11/14
I needed this during logon on Windows machines to set the sound volume: [WayBack] NirCmd – Windows command line tool set-soundvolume-25-percent.bat:
:: requires https://www.nirsoft.net/utils/nircmd.html
:: 100% = 65535
nircmd setsysvolume 16000
Works on all Windows versions (7-10) I tested so far.
Via
There are way sexier ways to do this, but they were all too convoluted for the time I had to get this to work.
For the future:
–jeroen
Posted in Batch-Files, Development, Power User, Scripting, Software Development, Windows | Leave a Comment »
Posted by jpluimers on 2018/11/13
Since ping has a more predictible output over Windows versions than ipconfig, I use this in a batch file:
for /f "delims=[] tokens=2" %%a in ('ping -4 -n 1 %ComputerName% ^| findstr [') do set NetworkIP=%%a
echo Network IP: %NetworkIP%
Source: [WayBack] How do I get the IP address into a batch-file variable? – Stack Overflow
Thanks [WayBack] bruce965.
–jeroen
Posted in Batch-Files, Development, Scripting, Software Development | Leave a Comment »
Posted by jpluimers on 2018/10/23
For my archive bc.bat it finds Beyond Compare, then starts it with the given command line parameters. It prefers version 4 over version 3 and user settings over system settings:
:begin
@echo off
setlocal
IF /I %PROCESSOR_ARCHITECTURE% == amd64 goto :x64
IF /I %PROCESSOR_ARCHITEW6432% == amd64 goto :x64
goto :x86
:x64
:: OS is 64bit
set hkcuBaseKey=HKEY_CURRENT_USER\Software\Scooter Software\Beyond Compare
set hklmBaseKey=HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Scooter Software\Beyond Compare
goto :findBC
:x86
:: OS is 32bit
set hkcuBaseKey=HKEY_CURRENT_USER\Software\Scooter Software\Beyond Compare
set hklmBaseKey=HKEY_LOCAL_MACHINE\SOFTWARE\Scooter Software\Beyond Compare
goto :findBC
:findBC
:: https://gist.github.com/rojepp/634908
:: http://stackoverflow.com/questions/5369528/windows-batch-reg-query-key-value-to-a-variable-but-do-not-display-error-if-key
set SupportedBeyondCompareVersions=3, 4
for %%v in (%SupportedBeyondCompareVersions%) do (
for /f "usebackq tokens=2* delims= " %%c in (`reg query "%hkcuBaseKey% %%v" /v ExePath 2^>NUL`) do (
call :do set bcExe="%%d"
)
)
if not [%bcExe%]==[] goto :foundBC
for /f "usebackq tokens=2* delims= " %%c in (`reg query "%hkcuBaseKey%" /v ExePath 2^>NUL`) do (
call :do set bcExe="%%d"
)
if not [%bcExe%]==[] goto :foundBC
for %%v in (%SupportedBeyondCompareVersions%) do (
for /f "usebackq tokens=2* delims= " %%c in (`reg query "%hklmBaseKey% %%v" /v ExePath 2^>NUL`) do (
call :do set bcExe="%%d"
)
)
if not [%bcExe%]==[] goto :foundBC
for /f "usebackq tokens=2* delims= " %%c in (`reg query "%hklmBaseKey%" /v ExePath 2^>NUL`) do (
call :do set bcExe="%%d"
)
:foundBC
if [%bcExe%]==[] ( echo no bc.exe found in registry) else (
echo bcExe=%bcExe%
if exist %bcExe% start "Beyond Compare" %bcExe% %*
if not exist %bcExe% echo not found: [%bcExe%]
)
:exit
endlocal
:end
goto :eof
:do
::echo %*
call %*
goto :eof
–jeroen
Posted in Batch-Files, Beyond Compare, Development, Power User, Scripting, Software Development | Leave a Comment »