Create script for mklink command - windows

I want to create a bash file (.sh file) that creates a symlink using the dos mklink command.
mklink /D "path" "path"
Use this window command in bash
But when I tried that I got an error: mklink: command not found
How to solve that?

For use mklink in you Windows, read the information on ss64.com about.
Elevation
By default, only Administrators can create symbolic links. The security setting 'Create symbolic links' can be granted at: Configuration\Windows Settings\Security Settings\Local Policies\User Rights Assignment\*
Creating a symbolic link requires elevation, but from Windows 10 build 14972, symlinks can be created without needing to elevate the console as administrator - this does however require that you have Developer Mode enabled.
So, you can enable Developer Mode enabled
I prefer to apply a boot after any changes made to the system settings, this is my habit, so I restarted and typed:
C:\Users\ecker>mklink /D "%userprofile%\Documents\Call of Duty Black Ops II Saves" "C:\Program Files (x86)\Steam\steamapps\common\Call of Duty Black Ops II\players"
symbolic link created for C:\Users\ecker\Documents\Call of Duty Black Ops II Saves <<===>> C:\Program Files (x86)\Steam\steamapps\common\Call of Duty Black Ops II\players
C:\Users\ecker>mklink
Creates a symbolic link.
MKLINK [[/D] | [/H] | [/J]] Link Target
/D Creates a directory symbolic link. Default is a file
symbolic link.
/H Creates a hard link instead of a symbolic link.
/J Creates a Directory Junction.
Link Specifies the new symbolic link name.
Target Specifies the path (relative or absolute) that the new link
refers to.
Obs.: You can also activate Developer Mode for Windows 10 using PowerShell, also for cmd command line or batch file:
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\AppModelUnlock" /t REG_DWORD /f /v "AllowDevelopmentWithoutDevLicense" /d "1"
Some further reading:
[√] MkLink /ss64.com
[√] MkLink /MS Doc's
[√] Enable Your Device For Development /MS Doc's

Related

How to run exe available on shared drive using command prompt

I have a exe available on shared drive example "\domain.com\folder\test.exe"
How to access this path using command prompt and execute exe.
I tried "net use y: \domain.com\folder" command. It created network drive but dont know how to execute exe.
You can reference the executable directly using a UNC path wrapped in quotes, such as
"\\server\share\path\file.extention"
The \\ at the beginning denotes it's a UNC path wrapping it in quote sis just good practice in case there are any spaces in the path.
"\domain.com\folder\test.exe" will work UNLESS the test.exe file needs to have it's cmd environment local to the program itself because it doesn't properly set itself on run.
in which case you might prefer to use:
MKLINK /D "C:\Link" "\\domain.com\folder"
CD "C:\Link"
test.exe
OR
net use y: "\\domain.com\folder"
CD /D "Y:\"
test.exe
OR
PushD "\\domain.com\folder"
test.exe
PopD
OR
MKLINK /D "C:\Link" "\\domain.com\folder"
MKLINK /J "C:\Junction" "C:\Link"
CD "C:\Junction"
test.exe
I prefer this final method but only because I use some of the features windows enables on junctions in the Explorer details, and does not enable on symbolic links.

Batch | Why can't a script running in admin access other scripts? [duplicate]

I have a batch file that is in the same directory as the file I want to xcopy. But for some reason the file is not being found.
I thought that current directory was always where the batch file was located.
I run batch file as administrator. This occurs on a Windows 7 64-bit desktop computer.
Batch file:
#ECHO OFF
XCOPY /y "File1.txt" "File2.txt"
PAUSE
Error:
File not found - File1.txt
0 File(s) copied
Which directory is current working directory on starting a batch file with context menu item Run as administrator depends on User Account Control (UAC) setting for the current user.
This can be demonstrated with following small batch file C:\Temp\Test.bat:
#echo Current directory is: %CD%
#pause
With having selected in User Account Control Settings
Default - Notify me only when programs try to make changes to my computer
Don't notify me when I make changes to Windows settings
and using Run as administrator, Windows uses registry key
HKEY_CLASSES_ROOT\batfile\shell\runasuser\command
This registry key does not contain a default string for executing the batch file. Instead there is the string value DelegateExecute with the CLSID {ea72d00e-4960-42fa-ba92-7792a7944c1d}.
The result is opening a dialog window with title User Account Control and text:
Do you want to allow the following program to make changes to this computer?
Program name: Windows Command Processor
Verified publisher: Microsoft Windows
After confirmation by the user, Windows opens temporarily a new user session like when using on command line RunAs.
In this new user session the current working directory is %SystemRoot%\System32 on executing now the command defined in Windows registry with default string of key
HKEY_CLASSES_ROOT\batfile\shell\runas\command
which is:
%SystemRoot%\System32\cmd.exe /C "%1" %*
Therefore a console window is opened with title C:\Windows\System32\cmd.exe and the 2 lines:
Current directory is: C:\Windows\System32
Press any key to continue . . .
After hitting any key, batch execution finishes which results in closing cmd.exe which results in closing the user session.
But with having selected in User Account Control Settings
Never notify me when
Programs try to install software or make changes to my computer
I make changes to Windows settings
the behavior is different as the user has already elevated privileges.
Now Windows uses directly the command
%SystemRoot%\System32\cmd.exe /C "%1" %*
according to default string of key
HKEY_CLASSES_ROOT\batfile\shell\runas\command
in current user session.
The result is opening a console window also with title C:\Windows\System32\cmd.exe, but displayed in window is:
Current directory is: C:\Temp
Press any key to continue . . .
The current working directory of the parent process (Windows Explorer as desktop) is used for executing of the batch file because no switch to a different user session was necessary in this case.
PA has posted already 2 possible solutions in his answer which I replicate here with a small improvement (pushd with directory in double quotes) and with adding a third one.
Change current directory to directory of batch file using pushd and popd:
pushd "%~dp0"
%SystemRoot%\System32\xcopy.exe "File1.txt" "File2.txt" /Y
popd
This works also for UNC paths. Run in a command prompt window pushd /? for an explanation why this also works for UNC paths.
Use directory of batch file in source and destination specifications:
%SystemRoot%\System32\xcopy.exe "%~dp0File1.txt" "%~dp0File2.txt" /Y
Change working directory to directory of batch file using cd:
cd /D "%~dp0"
%SystemRoot%\System32\xcopy.exe "File1.txt" "File2.txt" /Y
This does not work for UNC paths because command interpreter cmd does not support a UNC path as current directory by default, see for example CMD does not support UNC paths as current directories for details.
The error message is very self explanatory. The file file1.txt is not found.
Because the file name does not include an absolute path, the system tries to find it on the current directory. Your current directory does not contain this file.
Your misconception is that the current directory is not the directory that contains the bat file. Those are two unrelated concepts.
You can easily check by adding this two commands in your bat file
echo BAT directory is %~dp0
echo Current directory is %CD%
you can notice they are different, and that there is a subtle difference in the way the last backslash is appended or not.
So, there are esentially two ways to cope with this problem
either change the current directory to match the expected one
pushd %~dp0
XCOPY /y "File1.txt" "File2.txt"
popd
or specify the full path in the command
XCOPY /y "%~dp0File1.txt" "%~dp0File2.txt"
For the sake of completeness and obscurity, I add another workaround, confirmed as working under Windows 8.1 and expected to work elsewhere, as it relies on documented functionality:
You can change the runas command definition keys
HKEY_CLASSES_ROOT\batfile\shell\runas\command and
HKEY_CLASSES_ROOT\cmdfile\shell\runas\command into
%SystemRoot%\System32\cmd.exe /S /C "(for %%G in (%1) do cd /D "%%~dpG") & "%1"" %*
Which results in the bat or cmd file starting in its containing directory when started using the runas verb, respectively the "Run as Administrator" menu entry.
What the additions to the original command exactly do:
cmd /S strips away first and last (double) quote in command string after /C
for %%G in (%1) do enumerates its single entry, the %1 argument,
making it available for expansion as %%G in the loop body; the letter is arbitrary but some may be "reserved"
%%~dpG expands to the drive and path of %%G, the ~ tilde stripping away quotes if present, which is why we add them back explicitly
cd /D changes both the drive and directory to its argument, and finally
& runs the second command "%1" %* regardless of success of the first one.
You can use pushd which will even support UNC paths, but a stray popd would land any script in the system32 directory, not a behavior I would be fond of.
It should be possible to do this for the exefile entry as well, but frankly, I'd rather live with the inconsistency than to attempt this on my system, as any error there could break a lot.
Enjoy defeating the security mechanics of your operating system :)

Adding "Open Anaconda Prompt here" to context menu (Windows)

I'd like to add an option on my context menu (Windows 7 and 10) to open an Anaconda Prompt into the file location when I right-click the folder, but I can't figure out the right registry key.
Here's what I know how to do:
Add an item to the context menu that opens a normal command window at the folder location
Open an Anaconda prompt from cmd (run their "activate.bat" file)
What I can't figure out is how to combine these steps into a single registry key so I can open an Anaconda Prompt and then cd in that prompt to the current folder. But maybe I'm approaching this the wrong way.
Help from internet gurus is appreciated.
Run Registry Editor (regedit.exe)
Go to HKEY_CLASSES_ROOT > Directory > Background > shell
Add a key named AnacondaPrompt and set its value to Anaconda Prompt Here
Add a key under this key called command, and set its value to cmd.exe /K C:\Users\user\Anaconda3\Scripts\activate.bat change the location to wherever your Anaconda installation is located.
In recent Anaconda versions (I'm at conda 4.5.5) they have changed the behaviour and the shortcut to Anaconda Prompt, so the new procedure is in fact a bit simpler than described by bdforbes.
The new way to launch Anaconda Prompt in a folder is
cmd.exe /K %%USERPROFILE%%\AppData\Local\Continuum\Anaconda3\Scripts\activate.bat
pushd is to change the current directory, %V is the current directory, and /K is to run a command.
So the modified cwp2.py is not needed anymore. Put the following contents in a .bat-file and run as administrator to add the needed keys to the registry (a modified version of the gist posted by Thibaud Ruelle in the comments to the other answer)
REG ADD HKCR\Directory\Background\shell\Anaconda\ /ve /f /d "Anaconda Prompt Here"
REG ADD HKCR\Directory\Background\shell\Anaconda\ /v Icon /f /t REG_EXPAND_SZ /d %%USERPROFILE%%\\Anaconda3\\Menu\\Iconleak-Atrous-Console.ico
REG ADD HKCR\Directory\Background\shell\Anaconda\command /f /ve /t REG_EXPAND_SZ /d "%windir%\System32\cmd.exe pushd "%V" "/K" %%USERPROFILE%%\Anaconda3\Scripts\activate.bat %%USERPROFILE%%\Anaconda3"
REG ADD HKCR\Directory\shell\Anaconda\ /ve /f /d "Anaconda Prompt Here"
REG ADD HKCR\Directory\shell\Anaconda\ /v Icon /f /t REG_EXPAND_SZ /d %%USERPROFILE%%\\Anaconda3\\Menu\\Iconleak-Atrous-Console.ico
REG ADD HKCR\Directory\shell\Anaconda\command /f /ve /t REG_EXPAND_SZ /d "%windir%\System32\cmd.exe pushd "%V" "/K" %%USERPROFILE%%\Anaconda3\Scripts\activate.bat %%USERPROFILE%%\Anaconda3"
UPDATE: The answer by Filip S. might work better on more recent versions of Anaconda.
ANOTHER UPDATE: I fixed an issue with using this launcher in the drive root (e.g. C:\ or D:\). It's very minor: some whitespace has been added to the registry entry (relevant part: "%V ") so that the trailing backslash does not confuse Windows.
Original post
I also wanted this functionality, so I made it.
The key steps are outlined below, with the explanation further down.
Solution
Warning: Do not proceed unless you are comfortable editing the registry and are using a non-production system. And obviously don't run everything I tell you to, check that it's not doing anything nefarious. You don't know me!
1. Modify the Anaconda script that sets the working directory
Find the Anaconda script cwp.py (mine was in C:\Users\bdforbes\Anaconda3\) and copy it to cwp2.py in the same directory.
Modify cwp2.py to accept a target path as the second argument and change to that directory:
prefix = sys.argv[1]
cwd = sys.argv[2]
args = sys.argv[3:]
... (PATH setting code)
(REMOVE OLD LOGIC THAT CALLED os.chdir)
os.chdir(cwd)
sys.exit(subprocess.call(args, env=env))
Full code here: https://gist.github.com/bdforbes/9ef59cd01c22acefc20c5c92bd9550ae
2. Add the registry keys
In the registry, go to HKEY_CLASSES_ROOT\Directory\Background\shell\ and add a key Anaconda with default value "Open Anaconda Prompt Here", with a sub-key command with the following default value:
C:\Users\bdforbes\Anaconda3\pythonw.exe C:\Users\bdforbes\Anaconda3\cwp2.py C:\Users\bdforbes\Anaconda3 "%V " cmd.exe "/K" C:\Users\bdforbes\Anaconda3\Scripts\activate.bat C:\Users\bdforbes\Anaconda3
Add the same entries to HKEY_CLASSES_ROOT\Directory\shell\.
I've put a .reg file here, you just need to search replace bdforbes and replace it with your Windows account name. Don't run a .reg file without checking it first!
3. Use your fancy new context menu item
Right click on a folder. You should see the new entry there which will let you open a new Anaconda prompt.

the installation package could not be open batch file

I've been working on a batch file all day, that I can't get to work open through GPO (another day, another question). So I decided to do it manually with every computer. I have two exe's and one MSI. The exe's work perfectly fine. They get installed, and it all works out. The MSI, however, doesn't. It gives me the error: the installation package could not be opened. Verify that the package exists and that you can access it, or contact the application vendor to verify that this is a valid Windows Installer package.
Now when I go to the network share and use it from there, it works perfectly fine. So there must be an issue with my code.
Here's the code:
#echo off
IF NOT EXIST "C:\Program Files (x86)\Citrix\ICA Client\" (
pushd "\\KOPI-DC01\ACCURO Cloudwerx\ACCURO\1\"
.\CitrixReceiver-4.4.1000.exe /silent
)
IF NOT EXIST "C:\Program Files (x86)\triCerat\Simplify Printing\ScrewDrivers Client v4\" (
pushd "\\KOPI-DC01\ACCURO Cloudwerx\ACCURO\2\"
msiexec.exe /i ".\Screwdriver.msi"
)
IF NOT EXIST "C:\Program Files\Cloudwerx\CloudwerxPlugin\" (
pushd "\\KOPI-DC01\ACCURO Cloudwerx\ACCURO\3\"
.\cloudwerx-setup.exe /silent
)
pause
Any help would be greatly appreciated, thanks.
I am guessing that your problem is the distinction in powershell between the current location (set by the pushd command) and the working directory (unaffected by the pushd command). You can see the working directory of the powershell process using the [Environment]::CurrentDirectory property:
# C:\> [Environment]::CurrentDirectory = "c:\"
# C:\> [Environment]::CurrentDirectory
c:\
# C:\> pushd C:\Temp
# C:\Temp> [Environment]::CurrentDirectory
c:\
# C:\Temp> Get-Location
Path
----
C:\Temp
WHat is probably happening is that msiexec.exe is using the working directory (i.e. [Environment]::CurrentDirectory) and not the current powershell location at invocation. I would just specify the full path to msiexec:
msiexec.exe /i "\\KOPI-DC01\ACCURO Cloudwerx\ACCURO\2\\Screwdriver.msi"
MSI installation packages build with an older WIX utility would throw the error whenever installation was attempted from a batch script that was accessed on a shared drive using UNC path instead of a mapped drive letter. On the other hand whenever the batch file was executed with a mapped drive letter the installation would work normally.
I'm not blaming WIX here because I'm not certain whether they are responsible. I'm just describing symptoms here. It might just be the result of invoking plain vanilla Windows batch script that in turn executes msiexec with a bunch of command line parameters.

pushd \\network\path returns CMD does not support UNC paths as current directories

I was using a batch file to access some files from network.
I am using
pushd \\Network\path
to navigate to the networked directory to initiate some scripts
My bat file was working perfectly fine till this morning I saw "cmd returning CMD does not support UNC paths as current directories".
I have seen this error message when using cd instead of pushd to navigate to a network directory but I can figure out why I am getting error for a previously working bat file.
And I did make sure that network location was online and accessible other way round.
Be sure to also check that you haven't just run out of drive letters. If you have a rogue script that isn't using popd after it's done with the drive, or is crashing before it gets to popd, you can end up with a bunch of garbage mapped drive. Easy to check as they'll show up in net use and "My Computer". For some reason cmd will give this cryptic error (CMD does not support UNC paths as current directories.) instead of telling you it ran out of drives to map to.
From pushd /?:
If command extensions are enabled the PUSHD command accepts network
paths in addition to the normal drive letter and path. If a network
path is specified, PUSHD will create a temporary drive letter that
points to the specified network resource and then change the current
drive and directory.
Do any mapped drives show up in net use after pushd is executed?
If you're not getting some kind of network authorization error, make sure command extensions are enabled (I'm not sure why they wouldn't be.)
cmd /x will enable extensions for the current CMD session. Try that prior to executing your batch script.
The "main switch" is in HKEY_CURRENT_USER\Software\Microsoft\Command Processor.
EnableExtensions should be (DWORD) 1

Resources