@britechguy It's not a scripting language, it's a programming language and if you're writing it outside of an IDE with tab completion you're doing it wrong.
Inside the correct scope, the code is produces is more verbose, but MUCH easier to read and less prone to error.
The problem is... well... as you no doubt well know... WE TYPE THIS JUNK IN MANUALLY A LOT!
And creating files full of code snippets you have to copy / paste to keep my hair in my head is not only inefficient, but infuriating.
But I have no idea how CMD.EXE could be expanded to do all the things Powershell can do, and if you tried to abbreviate everything, the code would be illegible... because the same script can be manipulating stuff on the local machine, a domain controller, AND an M365 tenant at the same time. That's BONKERS, but what powershell was designed for.
Well, no one has ever ranted as much, or as consistently, as I have about the need to comment code. But I don't find what PowerShell does to be anything even kinda-sorta like "self documenting" due to it verbosity. I don't, at all, find it much easier to read or less error prone. The way I'll describe writing PowerShell code is that it's akin to what reading Henry James is like for me: By the time I get to the end of the sentence I can't remember a big part of what came at the beginning or middle. It's just plain messy.
I learned, many years ago and the very hard way, that being "too clever by half" when you've written code (in that case, in C, and insanely compact rather than verbose) and not commenting what you did will come back to bite you in the ass when you cannot unravel it later when you revisit what you, yourself, wrote.
One of the things I wanted/needed to do for my blind and VI clients was to enable sound in Windows Safe Mode, which I have never understood why it isn't to begin with. Here's one of the .BAT files (I'm not going to argue file extensions here, I was used to .BAT and used it) for enabling that:
-----------------------
@echo off
REM Get the path to the current folder in which this is running. This will include the terminal backslash on the path. See the
REM documentation for the FOR command near the end for details on the series of environment variables using tilde and letter(s)
SET CURDIR=%~dp0
SET LOGFILE=%CURDIR%GetSafeModeSound.log
REM Check to see if the keys needed already exist. Each will be checked in succession, and if it doesn't exist then
REM attempt to create it and set its value.
REM The key {4D36E96C-E325-11CE-BFC1-08002BE10318} is the first that must be present.
REG QUERY HKLM\SYSTEM\CurrentControlSet\Control\SafeBoot\Network\{4D36E96C-E325-11CE-BFC1-08002BE10318} > %LOGFILE% 2>&1 && (
ECHO.
ECHO The master key to allow sound in Safe Mode is already present on your system. Will attempt to set ownership, permissions, and value to allow sound in safe mode.
ECHO If that is successful, will continue on to check for presence or absence of other related keys.
) || (
ECHO.
ECHO The QUERY on the master sound key, {4D36E96C-E325-11CE-BFC1-08002BE10318}, indicates it is absent.
ECHO Now attempting to create it and set its value.
REG ADD HKLM\SYSTEM\CurrentControlSet\Control\SafeBoot\Network\{4D36E96C-E325-11CE-BFC1-08002BE10318} ^
/ve /t REG_SZ /d "Sound, video and game controllers" /f >> %LOGFILE% 2>&1 && (
ECHO.
ECHO {4D36E96C-E325-11CE-BFC1-08002BE10318} key created and value set successfully.
ECHO Now proceeding to check for presence or absence of other related keys.
) || (
ECHO.
ECHO Error creating necessary key and setting value. Sound will not be available in Safe Mode. Aborting.
PAUSE
EXIT 1
)
)
REM Find out whether we're running on a 64-bit or 32-bit system, and choose SetACL executable accordingly
REM The environment variable %PROGRAMFILES(x86)% is only defined on 64-bit systems, otherwise it's a 32-bit system
IF DEFINED PROGRAMFILES(x86) (
SET EACLCMD="%CURDIR%64-bit\SetACL.exe"
) ELSE (
SET EACLCMD="%CURDIR%32-bit\SetACL.exe"
)
REM Use the appropriate SetACL utility from
https://helgeklein.com to reset the owner of the key that needs to be deleted and recreated
REM so that it is owned by the Administrators group (represented by the SID S-1-5-32-544, which is the same no matter what
REM language is being used on the system being tweaked).
REM Stick a blank line in before whatever will be written out by the SetACL command
ECHO.
ECHO Setting ownership on {4D36E96C-E325-11CE-BFC1-08002BE10318} registry key to Administrators Group
%EACLCMD% ^
-on "HKLM\SYSTEM\CurrentControlSet\Control\SafeBoot\Network\{4D36E96C-E325-11CE-BFC1-08002BE10318}" ^
-ot reg -actn setowner -ownr "n:S-1-5-32-544"
IF %ERRORLEVEL% NEQ 0 (
ECHO.
ECHO SetACL returned error code: %ERRORLEVEL%
ECHO Registry key ownership could not be set to Administrator Group. Aborting after message is presented.
PAUSE
EXIT 1
)
REM Now use SetACL to grant Full Control to the Administrators group on the key so that it can be deleted and re-created
REM Stick a blank line in before whatever will be written out by the SetACL command
ECHO.
ECHO Setting Full Control on {4D36E96C-E325-11CE-BFC1-08002BE10318} registry key to Administrators Group
%EACLCMD% ^
-on "HKLM\SYSTEM\CurrentControlSet\Control\SafeBoot\Network\{4D36E96C-E325-11CE-BFC1-08002BE10318}" ^
-ot reg -actn ace -ace "n:S-1-5-32-544;p:full"
IF %ERRORLEVEL% NEQ 0 (
ECHO.
ECHO SetACL returned error code: %ERRORLEVEL%
ECHO Registry key permission could not be set to Full Control for Administrator Group. Aborting after message is presented.
PAUSE
EXIT 1
)
REM Stick a blank line in before whatever will be written out by the SetACL command
ECHO.
ECHO Deleting {4D36E96C-E325-11CE-BFC1-08002BE10318} registry key prior to recreating it with the needed value
REG DELETE HKLM\SYSTEM\CurrentControlSet\Control\SafeBoot\Network\{4D36E96C-E325-11CE-BFC1-08002BE10318} /va /f >> %LOGFILE% 2>&1
IF %ERRORLEVEL% NEQ 0 (
ECHO.
ECHO Registry key Delete failed, returning error %ERRORLEVEL%. Aborting after message is presented.
PAUSE
EXIT 1
)
REM Add the key back, with the (Default) value set to be "Sound, video and game controllers"
REM This will allow sound to be on when booting into Safe Mode on the computer
ECHO.
ECHO Creating {4D36E96C-E325-11CE-BFC1-08002BE10318} registry key with value of "Sound, video and game controllers"
REG ADD HKLM\SYSTEM\CurrentControlSet\Control\SafeBoot\Network\{4D36E96C-E325-11CE-BFC1-08002BE10318} ^
/ve /t REG_SZ /d "Sound, video and game controllers" /f >> %LOGFILE% 2>&1
IF %ERRORLEVEL% NEQ 0 (
ECHO.
ECHO Registry key Add failed, returning error %ERRORLEVEL%. Aborting after message is presented.
PAUSE
EXIT 1
)
REG QUERY HKLM\SYSTEM\CurrentControlSet\Control\SafeBoot\Network\AudioEndpointBuilder >> %LOGFILE% 2>&1 && (
ECHO.
ECHO The AudioEndpointBuilder key is already present on your system.
ECHO Now proceeding to check for presence or absence of other related keys.
) || (
ECHO.
ECHO The QUERY on the AudioEndpointBuilder key indicates it is absent on your system.
ECHO Now attempting to create it and set its value.
REG ADD HKLM\SYSTEM\CurrentControlSet\Control\SafeBoot\Network\AudioEndpointBuilder ^
/ve /t REG_SZ /d "Service" /f >> %LOGFILE% 2>&1 && (
ECHO.
ECHO AudioEndpointBuilder key created and value set successfully.
ECHO Now proceeding to check for presence or absence of other related keys.
) || (
ECHO.
ECHO Error creating AudioEndpointBuilder key and setting value. Sound will not be available in Safe Mode. Aborting.
PAUSE
EXIT 1
)
)
REG QUERY HKLM\SYSTEM\CurrentControlSet\Control\SafeBoot\Network\AudioSrv >> %LOGFILE% 2>&1 && (
ECHO.
ECHO The AudioSrv key is already present on your system.
ECHO Now proceeding to check for presence or absence of other related keys.
) || (
ECHO.
ECHO The QUERY on the AudioSrv key indicates it is absent on your system.
ECHO Now attempting to create it and set its value.
REG ADD HKLM\SYSTEM\CurrentControlSet\Control\SafeBoot\Network\AudioSrv ^
/ve /t REG_SZ /d "Service" /f >> %LOGFILE% 2>&1 && (
ECHO.
ECHO AudioSrv key created and value set successfully.
ECHO Now proceeding to check for presence or absence of MMCSS key, the final one needed.
) || (
ECHO.
ECHO Error creating AudioSrv key and setting value. Sound will not be available in Safe Mode. Aborting.
PAUSE
EXIT 1
)
)
REG QUERY HKLM\SYSTEM\CurrentControlSet\Control\SafeBoot\Network\MMCSS >> %LOGFILE% 2>&1 && (
ECHO.
ECHO The MMCSS key is already present on your system.
ECHO All key checks complete. Sound should be available when booting into Safe Mode with Networking.
) || (
ECHO.
ECHO The QUERY on the MMCSS key indicates it is absent on your system.
ECHO Now attempting to create it and set its value.
REG ADD HKLM\SYSTEM\CurrentControlSet\Control\SafeBoot\Network\MMCSS ^
/ve /t REG_SZ /d "Service" /f >> %LOGFILE% 2>&1 && (
ECHO.
ECHO MMCSS key created and value set successfully.
ECHO All key checks complete. Sound should be available when booting into Safe Mode with Networking.
) || (
ECHO.
ECHO Error creating MMCSS key and setting value. Sound will not be available in Safe Mode. Aborting.
PAUSE
EXIT 1
)
)
ECHO.
ECHO Registry configured successfully for sound in Safe Mode with Networking.
PAUSE
EXIT 0
------------------------
Whether one considers it elegant/good/well written technically or not, between the REMarks and the ECHOs of what's happening and when that code should be comprehensible to anyone that picks it up.
The same is true for some custom VBA scripting I did for a client to automate clicking a link when specific messages are received in Outlook. I've had to revisit that code as the text of the messages being watched for has been changed over time. Had I not commented exactly what I was doing, and how, I could never have updated it with ease. I learned what I know about VBA in order to complete that job and I've forgotten almost all of it. But I can still pick up that code and, from the comments, know what my intent was and what the code chunks that follow the comment chunks do.
I always keep that in mind when writing anything, even years after I was doing so on a day-to-day basis.