Jump to content
nukecad

CMD Syntax oddity?

Recommended Posts

I came across a syntax oddity today in the Command Line that I can't explain/understand.

I'm intending to write a batch file to do some registry manipulation so as a start I wanted to query some keys/entries.
(Eventually I'll want to remove certain entire keys depending on the contents of a particular entry).

Reg Query is the first command for the job; but here is where the oddity showed up.

I was running an elevated command window.

First attempt was:

reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Profiles\"

Which gave an error - 'The system was unable to find the specified registry key or value'.

I just couldn't get it to work until (by mistake) I entered:

reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Profiles\

Which sucessfully listed the 16 currently defined profile keys.
Note the oddity? There is an opening quotation mark but no closing quotation mark enclosing that path.

Normally I would expect to see an error if the quotes were mismatched, not the other way round.

All other variations on reg query that I tried do need the closing quotation mark. eg.

reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Profiles\{DA79D4DD-2ADF-4019-9EE8-833B012E44BF}" /v ProfileName

Will list the entry 'ProfileName' in the specified key, along with it's type and data.

However if I attempt to do a - reg query "HKML\..." /f "search_pattern"  on the parent path then it gives an 'error key or value not found' again, with or without mismatching quotes enclosing the path.
(and yes the search pattern does exist).

Can anyone point me to an explanation for this odd (to me) syntax that might help me understand what is going on?

BTW, This is Windows 10 1903.

Edited by nukecad

Share this post


Link to post
Share on other sites

I'd bet the problem is the \ at the end of the key name.  It shouldn't be there and the name of the key/path you're querying should just end with the last letter of the path name (and the quotes should be around it if there are any spaces in the path, otherwise you can leave them out though I personally always include them just to be safe).

Try this:

reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Profiles"

Edited by exile360

Share this post


Link to post
Share on other sites

By the way, if you want the output of the entire key's contents including any subkeys and values/value data then use the /s switch at the end leaving a space between the end of the key/path and the /s.  And don't forget to place quotation marks around the value when querying a specific value name using the /v switch again, if the value name contains any spaces (and you can use them even if it doesn't; it's a good habit to get into to avoid errors when paths/names do have spaces).

Edited by exile360

Share this post


Link to post
Share on other sites

Cheers exile,

I don't do much programming or scripting these days and when I did it was usually not on Windows.

I was aware of the other switches and syntax but was just keeping the above simple.

I had tried with wildcards after the trailing \ (which didn't work) but hadn't thought to leave it off altogether.

I guess that the parser was seeing it as an alternative closing quote mark. That still seems odd to me.

I'll have another play later.

Share this post


Link to post
Share on other sites

Yeah, I don't think it accepts wildcards by default (that's what the /s switch is for I believe).  I assume you know how to do output to a text file using > and >> or of course you can pipe the output using |.

Share this post


Link to post
Share on other sites

What I'm doing is removing old "Network #" profiles.

I tether to my phone quite a bit and Win 10 keeps insisting on creating new network profiles when you do that. Currently I manually clear them down now and again using regedit. It's not realy needed but I like to keep things tidy.

TBH it would probably be easier to do in Powershell, as most things are, but I eventually want to add it to an existing junk cleaning batch file.

I've looked at converting the existing batch to PS and calling it from a batch so it can be double clicked, but that would then mean a UAC warning to elevate it.

Share this post


Link to post
Share on other sites

It may be easier with a .reg file.  You could delete the entire key then re-add the ones for your network profiles that are persistent.  Just grab an export of the reg key from regedit after it's been cleaned up then edit the export to include a first line that wipes out the entire thing so that it gets emptied then recreates the ones you want to keep around.

Share this post


Link to post
Share on other sites

You could then import the reg via a batch file and use the Task Scheduler to schedule a task to do the import with highest privileges (no UAC prompt) and schedule it to occur whenever you want like on system startup or just have it there to run on demand from the Task Scheduler.

Share this post


Link to post
Share on other sites

RE:  https://ss64.com/nt/reg.html

Backslash characters

The REG command will interpret \ as an escape for the character that immediately follows it.
To include a quote mark (") in the data, prefix it with the escape character e.g. Here is \" a quote

This can cause problems with quoted directory paths because \" at the end of the line will be escaped.

To save a directory path with a trailing backslash (\) requires adding a second backslash to 'escape' the escape
so for example instead of "C:\My Docs\" use "C:\My Docs\\"

 

This will work...

reg query "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Profiles\\"

 

Share this post


Link to post
Share on other sites

Thanks David.

That does make sense to me now.

Exile360,

Yes there is usually more than one way to skin cat. Pay your money (or DIY) and take a choice.

Some of my browser junk cleaner works on the simple basis of delete the whole cache/history/db/whatever and let the browser recreate it when opened again It could be fancier but often simple works well enough.

Share this post


Link to post
Share on other sites

If your configuration doesn't change frequently you could also get it set the way you prefer then set that key to deny access even to the SYSTEM account that way the system/software cannot make any changes to it, though if you ever needed to connect to any new networks you'd need to change permissions on it again.  If your setup is relatively static then it might be worth considering.

Share this post


Link to post
Share on other sites
19 hours ago, exile360 said:

If your configuration doesn't change frequently you could also get it set the way you prefer then set that key to deny access even to the SYSTEM account that way the system/software cannot make any changes to it, though if you ever needed to connect to any new networks you'd need to change permissions on it again.  If your setup is relatively static then it might be worth considering.

I don't think that would work.

When you tether the phone to USB Win 10 sometimes creates a new network profile. (We had a previous thread about why it does this).
If you denied the system access to create a new reg key for the profile then I suspect that the tethering would simply fail.

For the moment, now that I've got what was going on with that syntax, and the unintended escaping, sorted out I'm happy to continue with it in the command window.
I've sucessfully got it listing only the tethered network profiles, so just have to carry on extracting the profile designations and deleting the profiles.
There are probably easier ways to do it, but it gives me something to play at, plus practice writing batch files.

It should be pretty simple once I've written it.
If I run the cleaner at the end of each session, as I normally do, then there will (should) only be the one tethered network to remove.
(Or none if I've been using wifi).
Windows will just create Network 2 each time I tether, and then if it has I'll remove it again when I shut down.
(Now if the key was called Network 2 rather than a hex string that would be even easier).
Doing that it should never get above Network 2

Edited by nukecad

Share this post


Link to post
Share on other sites

Ah, gotcha.  Yeah, if it's not static then it definitely won't work and would be more of a hassle than it's worth.  The create and cleanup method likely is the best approach in this case.

It would be cool if you could somehow schedule the cleanup to kick off automatically whenever that device is connected/once those entries are created.  That would automate the whole thing and only trigger it when required.  There might be a way to do it but I'm not certain.  I know the Task Scheduler has a lot of triggers, but the closest would likely be when a particular program is run, not when a specific hardware device is connected but you never know.

Share this post


Link to post
Share on other sites

As said in that other thread I mentioned; the new network profile creation seems to be somehow tied in with Defender definition updates, it doesn't always happen everytime you tether.

It should be simple enough to schedule a cleaning routine to check for tethering profiles on startup, and remove any that are there.
Windows will then just create a new Network 2 profile next time you tether.

There again as I aways clear the browser stuff once I've finished a session it should also be easy enough to add it to my cleaning batch file.
My cleaner already does that for some other stuff that is not browser related - clear some recent lists, logfiles, solitaire scores, empty the recycle bin, etc.

I started writing my own, personalised, cleaner because I got disgruntled with the way CCleaner has turned into dumbed-down nagware.
Being targeted at just what I want to clean on my particular laptop means it's finished the cleaning faster than CC would have loaded it's GUI.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...

Important Information

This site uses cookies - We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.