Archive for the ‘Batch-Files’ Category
Posted by jpluimers on 2021/09/09
I knew I could run shell:startup and similar shortcuts from the Explorer address bar or the Windows-R “run” prompt.
First I learned that via [WayBack] tablet – How to set Google Chrome to automatically open up and in full screen – Super User.
Then via [WayBack] “shell:startup” – Google Search, I found [WayBack] Location of the Startup folder in Windows 10.
It took a while before I realised you can also run them from the command-prompt, batch-files or PowerShell scripts prepending them with start:
start shell:startup
That one will open a new explorer window in the user startup folder from either the command-prompt, a batch file or PowerShell script..
The shell: shortcuts can contain spaces. So for instance there is shell:common startup that opens the common startup folder.
Starting it from the command prompt, batch file or PowerShell script is different: because of the spaces you will get the error on the right unless you add double quotes:
start "shell:common statartup"
All shell: commands that you can run in the same way: double quotes work for both the ones requiring spaces and the simple ones nor requiring spaces.
Virtually each new Windows version (even most Windows 10 major builds) gets new shell: commands.
A good source with an up-to-date and historically accurate of shell: commands list is at [WayBack] Shell Commands to Access the Special Folders in Windows 10/8/7/Vista/XP » Winhelponline,
You can get the current list by recursively enumerating the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FolderDescriptions registry key, which consists of a list of Explorer folder GUIDs having Name, ParentFolder and RelativePath value names.
–jeroen
Read the rest of this entry »
Posted in Batch-Files, Console (command prompt window), Development, Power User, Scripting, Software Development, Windows | Leave a Comment »
Posted by jpluimers on 2021/08/09
[WayBack] How to turn on automatic logon in Windows
Describes how to turn on the automatic logon feature in Windows by editing the registry.
Most archivals of the above post fail with a 404-error after briefly flashing the content, but this particular one usually succeeds displaying.
It is slightly different from the one referenced in my blog post automatic logon in Windows 2003, and because of the archival issues, I have quoted most of it below.
A few observations, at least in Windows 10 and 8.1:
- Major Windows 10 upgrades will disable the autologon: after each major upgrade, you have to re-apply the registry patches.
- If the user has a blank password, you can remove the DefaultPassword value.
- Empty passwords allow local logon (no network logon or remote desktop logon), no network access and no RunAs, which can actually help improve security. More on that in a later blog post
- For a local machine logon, you do not need the DefaultDomainName value either (despite many posts insisting you need them), but you can technically set it to the computer name using
reg add "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Winlogon" /v DefaultDomainName /t REG_SZ /d %ComputerName% /f
- If another user logs on and off, the values keep preserved, so after a reboot, the correct user automatically logs on
- you need a full reboot cycle for this to take effect
- The AutoLogon tool does not allow blank passwords
I wrote a batch file enable-autologon-for-user-parameter.bat that makes it easier:
if [%1] == [] goto :help
:enable
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /v AutoAdminLogon /t REG_SZ /d 1 /f
:setUserName
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /v DefaultUserName /t REG_SZ /d %1 /f
:removePasswordIfItExists
reg delete "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /v DefaultPassword /f
if [%2] == [] goto :eof
:setPassword
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /v DefaultPassword /t REG_SZ /d %2 /f
goto :eof
:help
echo Syntax:
echo %0 username password
The article quote:
Read the rest of this entry »
Posted in Batch-Files, Development, Microsoft Surface on Windows 7, Power User, Scripting, Software Development, Windows, Windows 10, Windows 7, Windows 8, Windows 8.1, Windows 9, Windows Server 2003, Windows Server 2003 R2, Windows Server 2008, Windows Server 2008 R2, Windows Server 2012, Windows Server 2012 R2, Windows Server 2016, Windows Vista, Windows XP | Leave a Comment »
Posted by jpluimers on 2021/08/03
When configuring a web-based kiosk for someone with Alzheimer’s disease, I wanted to start Chrome in full-screen kiosk mode.
Chrome full-screen kiosk mode
The secret for full-screen kiosk mode is to pass the -start-fullscreen command-line option. Thanks [WayBack] User ginomay89 – Super User for answering that in [WayBack] tablet – How to set Google Chrome to automatically open up and in full screen – Super User.
Finding chrome
At first I thought about differentiating on the chrome.exe location that you can find in the registry. This turned out to be depending on how you install Chrome:
- locally for the current user by a non-local-administrator user (by default the location is under
%LocalAppData%)
- globally for all users by a local-administratator user (by default is under
%ProgramFiles(x86)%)
Oddly, there is no way (not even by denying UAC elevation!) for a local administrator to install Chrome for only the current user.
This is odd, as when non-local-administrator denies UAC, the installation is locally to the user.
Then I remembered there are two ways for Windows to find an application
- On the
PATH
- Via the start menu (which includes anything on the
PATH)
The cool thing is that the start command does the latter, so I came up with this batch file that starts chrome with the -start-fullscreen parameter that will initiate kiosk mode with the default chrome settings:
start "Chrome Kiosk Mode" chrome --start-fullscreen
In case I want to compare the registry settings
Basically sorting out the registry settings would mean parsing the references to chrome.exe (often with extra parameters) in the below registry key/value-name pairs.
One day I might need to do this for different reasons, but currently the start trick suffices.
Read the rest of this entry »
Posted in Batch-Files, Development, Power User, Scripting, Software Development, Windows | Leave a Comment »
Posted by jpluimers on 2021/07/22
Based on zzz but filters current user, and listener session.
:: https://stackoverflow.com/questions/36715033/how-to-logoff-all-users-on-windows-from-command-line-as-a-domain-administrator
:: The findstr bit filters out the current session (starts with ">") and session 65536 (which is the listener)
for /f "skip=2 tokens=2,3 delims= " %%a in ('query session ^| findstr /v /b ">" ^| findstr /v "65536 Listen"') DO (
echo %%a|findstr /xr "[1-9][0-9]* 0" >nul && (
logoff %%a
)
echo %%b|findstr /xr "[1-9][0-9]* 0" >nul && (
logoff %%b
)
)
goto :eof
–jeroen
Posted in Batch-Files, Development, Scripting, Software Development, Windows Development | Leave a Comment »
Posted by jpluimers on 2021/07/15
[WayBack] How do I get the application exit code from a Windows command line? – Stack Overflow solutions below.
Note they ONLY work when nobody sets the ERRORLEVEL environment variable.
- – [WayBack]
-
something.exe
echo Exit Code is %errorlevel%
– [WayBack] Samuel Renkert
-
start /wait something.exe
echo %errorlevel%
– [WayBack] Gary
-
@echo off
my_nify_exe.exe
if %ERRORLEVEL% EQU 0 (
echo Success
) else (
echo Failure Reason Given is %errorlevel%
exit /b %errorlevel%
)
– [WayBack] Curtis Yallop
- It’s worth noting that .BAT and .CMD files operate differently.Reading https://ss64.com/nt/errorlevel.html it notes the following:
There is a key difference between the way .CMD and .BAT batch files set errorlevels:
An old .BAT batch script running the ‘new’ internal commands: APPEND, ASSOC, PATH, PROMPT, FTYPE and SET will only set ERRORLEVEL if an error occurs. So if you have two commands in the batch script and the first fails, the ERRORLEVEL will remain set even after the second command succeeds.
This can make debugging a problem BAT script more difficult, a CMD batch script is more consistent and will set ERRORLEVEL after every command that you run .
This was causing me no end of grief as I was executing successive commands, but the ERRORLEVEL would remain unchanged even in the event of a failure. – [WayBack] RockDoctor
–jeroen
Posted in Batch-Files, Development, Scripting, Software Development | Leave a Comment »
Posted by jpluimers on 2021/07/14
You can use:
IF "%~1" == "" GOTO MyLabel
to strip the outer set of quotes. In general, this is a more reliable method than using square brackets because it will work even if the variable has spaces in it.
Source: [WayBack] jamesdlin answering on [WayBack] windows – What is the proper way to test if a parameter is empty in a batch file? – Stack Overflow
The tilde (~) strips out double quotes from the command as per
C:\>help for | findstr "~"
%~I - expands %I removing any surrounding quotes (")
%~fI - expands %I to a fully qualified path name
%~dI - expands %I to a drive letter only
%~pI - expands %I to a path only
%~nI - expands %I to a file name only
%~xI - expands %I to a file extension only
%~sI - expanded path contains short names only
%~aI - expands %I to file attributes of file
%~tI - expands %I to date/time of file
%~zI - expands %I to size of file
%~$PATH:I - searches the directories listed in the PATH
%~dpI - expands %I to a drive letter and path only
%~nxI - expands %I to a file name and extension only
%~fsI - expands %I to a full path name with short names only
%~dp$PATH:I - searches the directories listed in the PATH
%~ftzaI - expands %I to a DIR like output line
values. The %~ syntax is terminated by a valid FOR variable name.
–jeroen
Posted in Batch-Files, Development, Power User, Scripting, Software Development, Windows | Leave a Comment »
Posted by jpluimers on 2021/06/22
Windows if full of undocumented gizmo’s, like find alternative for wc -l counting all lines in a file: [WayBack] File Line Count
Use FIND command to count file lines, store line count into a variable.
| Description: |
Running the FIND command with option /v and empty search string will find all lines
Running the FIND command with option /c will output the line count only.
The FOR command with option /f will parse the output, the line count in this case, and the set command put the line number into the cnt variable. |
| Script: |
1.
2.
3.
4.
|
set file=textfile.txt
set /a cnt=0
for /f %%a in ('type "%file%"^|find "" /v /c') do set /a cnt=%%a
echo %file% has %cnt% lines
|
|
| Script Output: |
Script Output |
textfile.txt has 50 lines
|
|
[WayBack] Stupid command-line trick: Counting the number of lines in stdin | The Old New Thing
Windows doesn’t come with wc,
but there’s a sneaky way to count the number of lines anyway:
some-command-that-generates-output | find /c /v ""
It is a special quirk of the find command
that the null string is treated as never matching.
The /v flag reverses the sense of the test,
so now it matches everything.
And the /c flag returns the count.
…
The reason dates back to the original MS-DOS
version of find.exe,
which according to the comments appears to have been written
in 1982.
And back then, pretty much all of MS-DOS was written in assembly
language.
…
Via: batch file line count – Google Search and [WayBack] windows – How to count no of lines in text file and store the value into a variable using batch script? – Stack Overflow
–jeroen
Posted in Batch-Files, Development, Scripting, Software Development, Windows Development | Leave a Comment »
Posted by jpluimers on 2021/06/10
One of the situations where setlocal enabledelayedexpansion comes on handy to replace \ with / which some unix based tools like better: [WayBack] string – Change backslash to forward slash in windows batch file – Stack Overflow
echo off
setlocal enabledelayedexpansion
for %%f IN ("C:\tools\workspace\*") DO (
set old=%%f
echo !old!
set new=!old:\=/!
echo !new!
echo.
)
Related, as it explains when the source and target replacements are in variables themselves: [WayBack] command line – String replacement in batch file – Stack Overflow, thanks Joey and Tom Warfield!
You can use the following little trick:
set word=table
set str="jump over the chair"
call set str=%%str:chair=%word%%%
echo %str%
The call there causes another layer of variable expansion, making it necessary to quote the original % signs but it all works out in the end.
…
Upvoting this answer because it works both ways, with the environment variable in either position, or in both the “before” and “after” positions:
set word=table
set str="jump over the chair"
call set str=%%str:chair=%word%%%
echo %str%
set word1=chair
set word2=desk
set str="jump over the chair"
call set str=%%str:%word1%=%word2%%%
echo %str%'
–jeroen
Posted in Batch-Files, Development, Scripting, Software Development | Leave a Comment »
Posted by jpluimers on 2021/05/19
From my batch file library; note this PowerShell wrapper does not support stdin, but that is OK for me.
For Windows 10 and up, sort /unique works too and supports stdin.
:: note: does not work on stdin, unliqke sort which does support standard input
:: http://secretgeek.net/ps_duplicates
PowerShell Get-Content %1 ^| Sort-Object ^| Get-Unique
:: note that for Windoww 10 and up, there is a sort /unique switch, but Windows versions below it do not.
:: https://superuser.com/questions/1316317/is-there-a-windows-equivalent-to-the-unix-uniq
Related:
–jeroen
Posted in Batch-Files, CommandLine, Development, PowerShell, PowerShell, Scripting, Software Development | Leave a Comment »
Posted by jpluimers on 2021/05/06
A while ago I found out that choco upgrade will install a package when it is not installed, and that choco upgrade all will upgrade all installed packages.
So I looked up the documentation of both choco install and choco upgrade (and choco update which will be deprecated) where I bolded parts of the upgrade command:
Given that upgrade can do both an upgrade and an install, I have switched all my install scripts to use upgrade in steaf of install.
Not all of the above commands accept the same set of [Wayback] Chocolatey Software Docs | Commands: Default Options and Switches
-?, --help, -h
Prints out the help menu.
-d, --debug
Debug - Show debug messaging.
-v, --verbose
Verbose - Show verbose messaging. Very verbose messaging, avoid using
under normal circumstances.
--trace
Trace - Show trace messaging. Very, very verbose trace messaging. Avoid
except when needing super low-level .NET Framework debugging. Available
in 0.10.4+.
--nocolor, --no-color
No Color - Do not show colorization in logging output. This overrides
the feature 'logWithoutColor', set to 'False'. Available in 0.10.9+.
--acceptlicense, --accept-license
AcceptLicense - Accept license dialogs automatically. Reserved for
future use.
-y, --yes, --confirm
Confirm all prompts - Chooses affirmative answer instead of prompting.
Implies --accept-license
-f, --force
Force - force the behavior. Do not use force during normal operation -
it subverts some of the smart behavior for commands.
--noop, --whatif, --what-if
NoOp / WhatIf - Don't actually do anything.
-r, --limitoutput, --limit-output
LimitOutput - Limit the output to essential information
--timeout, --execution-timeout=VALUE
CommandExecutionTimeout (in seconds) - The time to allow a command to
finish before timing out. Overrides the default execution timeout in the
configuration of 2700 seconds. '0' for infinite starting in 0.10.4.
-c, --cache, --cachelocation, --cache-location=VALUE
CacheLocation - Location for download cache, defaults to %TEMP% or value
in chocolatey.config file.
--allowunofficial, --allow-unofficial, --allowunofficialbuild, --allow-unofficial-build
AllowUnofficialBuild - When not using the official build you must set
this flag for choco to continue.
--failstderr, --failonstderr, --fail-on-stderr, --fail-on-standard-error, --fail-on-error-output
FailOnStandardError - Fail on standard error output (stderr), typically
received when running external commands during install providers. This
overrides the feature failOnStandardError.
--use-system-powershell
UseSystemPowerShell - Execute PowerShell using an external process
instead of the built-in PowerShell host. Should only be used when
internal host is failing. Available in 0.9.10+.
--no-progress
Do Not Show Progress - Do not show download progress percentages.
Available in 0.10.4+.
--proxy=VALUE
Proxy Location - Explicit proxy location. Overrides the default proxy
location of ''. Available for config settings in 0.9.9.9+, this CLI
option available in 0.10.4+.
--proxy-user=VALUE
Proxy User Name - Explicit proxy user (optional). Requires explicit
proxy (`--proxy` or config setting). Overrides the default proxy user of
''. Available for config settings in 0.9.9.9+, this CLI option available
in 0.10.4+.
--proxy-password=VALUE
Proxy Password - Explicit proxy password (optional) to be used with
username. Requires explicit proxy (`--proxy` or config setting) and
user name. Overrides the default proxy password (encrypted in settings
if set). Available for config settings in 0.9.9.9+, this CLI option
available in 0.10.4+.
--proxy-bypass-list=VALUE
ProxyBypassList - Comma separated list of regex locations to bypass on
proxy. Requires explicit proxy (`--proxy` or config setting). Overrides
the default proxy bypass list of ''. Available in 0.10.4+.
--proxy-bypass-on-local
Proxy Bypass On Local - Bypass proxy for local connections. Requires
explicit proxy (`--proxy` or config setting). Overrides the default
proxy bypass on local setting of 'True'. Available in 0.10.4+.
--log-file=VALUE
Log File to output to in addition to regular loggers. Available in 0.1-
0.8+.
–jeroen
Posted in Batch-Files, Chocolatey, Development, Power User, Scripting, Software Development, Windows | Leave a Comment »