Naming network drives from commandline rather than Windows Explorer - windows

I'm writing a batch file to map multiple servers to drives on my PC. Using the NET USE command, I have managed to map the drives successfully.
When viewed in Windows Explorer, each drive shows the letter assignment and the server name. I'd like to display a user-friendly plain-English name for each server in the Explorer view also (this is different from the volume label). I can right-click on each drive individually in Explorer and rename but this is a bit long-winded.
Is there any way that names can be assigned to the drives from the command prompt (and therefore from a batch file) rather than right-clicking and renaming in Explorer?

You can do it in Powershell like this:
$rename = new-object -ComObject Shell.Application
$rename.NameSpace("X:\").Self.Name = "DriveLabel"
Just replace the X with the drive-letter on which you want to set the label.
Alternatively if you donĀ“t want to use Powershell you can do the following:
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2\##computername#sharename" /f /v "_LabelFromReg" /t REG_SZ /d "DriveLabel"
Of course you have to replace computername with the servername, the sharename with the name of the share and the Drivelabel with your label

The "label" value for a mapped network drive is stored in the registry.
Look into HKEY_USERS\*SID*\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2. There should be a key that represents the mapped network drive. If there is a value called _LabelFromReg you have a custom label and if the value is not there then it uses the default label, such as, Windows (\server\share) (Z:).
You should also be able to use Group Policy Preference, and Network Folders and Shortcuts to get the labels you want.

Related

windows - create shortcut to printer with powershell

There are various questions and answers on SO explaining how to create a shortcut to the printer queue of a printer using powershell. They all use a shortcut destination like this: C:\Windows\System32\rundll32.exe printui.dll,PrintUIEntry /n Printername.
However, the shortcuts created like this do not have the right-click options like Scan which are visible when right-clicking the printer on the printer & devices page in the control panel. Right-clicking the printer on the Devices and Printers page reveals a Create Shortcut option, which creates a shortcut which has the same right-click options as the original item. (Dragging the printer to the desired destination folder works too)
The properties pages of the shortcuts created a) with powershell b) with the Create shortcut option also look quite different:
Thus, my question is: How do I create a shortcut that is equivalent to the shortcut created when using the right-click option Create Shortcut on a printer on the devices and printers page with powershell?
The correct shortcut does not point to rundll32.exe, it points to the printer in the shell namespace. This target is an item id list, not a filesystem path.
I don't know the native way to do this in Powershell. With P/invoke it would be SHParseDisplayName + IShellLink::SetIDList.
You would probably want to go the reverse way first; on a correct link, get its id list and call SHGetNameFromIDList(...,SIGDN_DESKTOPABSOLUTEPARSING,...). The returned string would look something like ::{GUIDHERE}\::{ANOTHERGUID}\MyPrinterName.
Thanks to Anders for bringing me onto the right track.
You can achieve it like this:
Get a list of all devices and printers:
$shell = New-Object -ComObject Shell.Application
$devices_and_printers = $shell.namespace("::{26EE0668-A00A-44D7-9371-BEB064C98683}\2\::{A8A91A66-3A7D-4424-8D24-04E180695C7A}")
$devices_and_printers.items() | select name,path
Create shortcut:
$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut("$env:userprofile\Desktop\My Printer.lnk")
$Shortcut.TargetPath = ($devices_and_printers.items() | where { $_.name -eq "My Printer Name" }).Path
$Shortcut.Save()

How to open special locations in windows explorer through command line?

In Windows Explorer, there are some special locations besides local paths, such as This PC, Quick Access, Network, or some remote location provided by net disks like OneDrive.
For local paths, I can simply use "explorer [location]" in cmd to open them. As for those special locations, is there a way to open them through command line? If not provided, is it possible to control Windows Explorer to navigate to these locations from an external program after it's launched?
(Assuming Windows 10) You can use "explorer shell:[folderName]" from the command prompt, such as:
explorer shell:ThisPcDesktopFolder
https://www.winhelponline.com/blog/shell-commands-to-access-the-special-folders/ has a good list.

Manipulating QNAP files starting with dot in name on Windows volumes

We have a QNAP NAS mounted on windows 10.
Any file on this volume which is created with a name starting with dot (like .cproject)
gets a hidden attribute which cant be changed neither in a command window (cmd and attrib.exe)
nor via explorer properties dialog.
However with explorer it is possible to rename, copy, move,remove the file.
Furthermore is possible to rename to a name not starting with dot and then sucessfully remove the hidden attribute.
Up to now I did not manage to do such operations in a windows batch file. (Neither with WIN10 nor WIN7)
n:\00\tst\ARM\applikation\fsbl>attrib .cproject
A H N:\00\tst\ARM\applikation\fsbl\.cproject
n:\00\tst\ARM\applikation\fsbl>ren .cproject w.cproject
The system cannot find the file specified.
n:\00\tst\ARM\applikation\fsbl>
What can I do?
Is it somehow possible to mimic what explorer can do?

Drive mapping unavailable when added via registry

I want to add drive mapping from my c# app for specific user on remote machine. I'm trying to do it by adding new registry key.
When I add new drive mapping in registry, adding new key in:
HKEY_USERS\< USER >\Network
so, for example, to map \some_path\all under R:\ I make new key:
HKEY_USERS\< USER >\Network\R
with following values:
such added drive is visible when I type
net use
in command line, but it's status is "Unavailable". It becomes "OK" after computer restart(and it's visible in Windows Explorer).
How can I achieve it without restarting computer? What process during restart changes drive status and makes it visible in Windows Explorer?
Or another approach: when i type add drive via:
net use R:\ \\some_path\all
it's immediately available in Windows Explorer and visible after net use command. What net use does beside adding new key to registry(as presented above)?

Windows 7 file extension association

I am referring specifically to windows 7.
I have code that associates a certain extension with my application as proposed by webJose on the following page:
What registry keys are responsible for file extension association?
(However i correctly write to HKEY_CURRENT_USER\Software\Classes instead of HKEY_CLASSES_ROOT as suggested)
The above works initially, or if there are no other programs associated with the extension. However after using the Windows 7 built-in "Choose default program..." (found under the file-right-click context menu under "Open with") it re-associates the extension with whatever new program you choose.
What happens at this point is that "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\\UserChoice" is changed by the system, and so the newly selected program takes over.
Running the above code, to regain control over the extension will not work. The only way to regain control, is by either:
Editing the UserChoice -> Progid value, which is not allowed (neither programmatically nor using regedit.exe - access denied).
Or deleting the UserChoice value and making sure your application is the first in the MRUList value under \OpenWithList (this can be achieved using regedit.exe but not programmatically)
My question is: Is there a way to achieve this programmatically? What registry values can be changed to regain control of an extension, after is associated with another program?
I know it might seem obvious that if a user through explorer sets the associated application to an extension, that it would be expected to do it the same way again to re-associate the extension to a different application.
The problem however is I have a button in my application that uses the above mentioned code to check for extension association with my application. Unfortunately with the above situation, my application displays a message confirming that the extension is already successfully associated when its not! So is there a way around this?
Deleting UserChoice should revert the default program to the standard file association keys (which starts with the ProgID in HKCU). Barring that you could also delete OpenWithList, which would be reverting with extreme prejudice.
Edit:
Check out Registry Key Security and Access Rights on MSDN, particularly the RegSetKeySecurity function. Remember that you'll need to grant yourself administrative control to the key before you can delete it.
Well regarding file assoc in Window 7 a new 'problem' araised.
It's one of this: You've to fight for your rights.
Assuming you like to run
REG.exe DELETE "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.mov\UserChoice" /f /va
You'll get ACCESS DENYED.
When you check security setting of the key in Regedit 'UserChoice' you'll see that there's a setting windows made for you, to deny 'set' for the current user. Well you maybe change/delete this setting in regedit and now you can delete UserChoice.
However for programmer/scripters that setting is a little bitchy since there are now real tools to set ACLs in the registry. However here some workaround that at allows to delete keys with ACCESS DENYED (of course this only works incase you've the right to change permissions):
ResetMovAssoc.cmd
::create 'empty.hiv'
REG ADD "HKCU\emptyKey" /f
REG SAVE "HKCU\emptyKey" empty.hiv /y
#REG DELETE "HKCU\emptyKey" /f >nul
::^-note you can add #[...] >nul to the other entries as well to run them quite
:: Delete Reg key by replacing it with an empty hiv
REG RESTORE "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.mov" empty.hiv
del empty.hiv
To summarize this the major thing here is REG RESTORE + Registry hive file containing just and empty key.
In Regedit that'll equivalent to Import' with a just an empty registry structure file (Note: that's a hive file and not a *.reg file).
My solution was not to try to delete the UserChoice key (only administrators can do it), but to delete the key the ProgId is pointing to. If the user made a choice in the past the ProgId has a value like Applications\*.exe. The non-admin can delete the key in a batch:
REG DELETE HKCR\Applications\*.exe /f
This might be a bit of a hack, but worked for me.
Also on Windows 10, this command can't work:
Reg.exe delete "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.m4v" /f
The solution is to delete the SID key by running a BAT file under TrustedInstaller (with NSudo, PowerRun, etc):
for /f "delims=\ tokens=2" %%A in ('reg query hku ^| findstr /i "S-1-5-21-" ^| findstr /v /i "_Classes"') do set SID=%%A
Reg.exe delete "HKU\%SID%\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.m4v" /f
P.S.: Thanks to #SecurityAndPrivacyGuru for sharing the SID detection command.

Resources