Powershell Remove Symbolic Link Windows - windows

I am having issues when removing SymbolicLinks which I have created with New-Item:
New-Item -ItemType SymbolicLink -Path C:\SPI -Target "C:\Users\Chino\Dropbox (Reserve Membership)\"
I need to modify the link because it has the wrong -Target, which should be:
New-Item -ItemType SymbolicLink -Path C:\SPI -Target "C:\Users\Chino\Dropbox (Reserve Membership)\SPI"
How to remove that link and assign a new one? Alternatively, how to update the target path of the existing link?

Calling Delete() on the corresponding DirectoryInfo object should do the trick:
(Get-Item C:\SPI).Delete()
New-Item -ItemType SymbolicLink -Path C:\SPI -Target "C:\Users\Chino\Dropbox (Reserve Membership)\SPI"

If you want to change the target path of the existing symbolic link C:\SPI from "C:\Users\Chino\Dropbox (Reserve Membership)\" to "C:\Users\Chino\Dropbox (Reserve Membership)\SPI\" you do not need to delete the link beforehand. Simply including the -Force parameter to overwrite the link works for me in PowerShell 5.1 on Windows 10 Pro v1603:
New-Item -ItemType SymbolicLink -Path C:\SPI -Target "C:\Users\Chino\Dropbox (Reserve Membership)\SPI" -Force

No way to update the symbolic link as far as I know. Gotta use CMD to remove symbolic link and you could then re-create it using your powershell script. You would do it like this in powershell.
cmd /c "rmdir C:\SPI"

Related

Windows : navigate into symlink

I created a symbolic link in windows and it's working fine :)
when I run:
Get-ChildItem myLink
I get:
d----- MyFolder
But when I run:
cd myLink
It shows error of null exception
I want to run a solution in this path
myLink\MyFolder\mySolution.sln
How can I run it?
Tnx.
I used
New-Item -Path $newFolder -ItemType SymbolicLink -Value $oldFolder -Force
Using mklink works fine:
mklink /d $newFolder $oldFolder

How to create a new alias in Windows Powershell for deleting multiple folders?

I want to create an alias in Windows PowerShell to delete multiple folders from the command line.
To remove more than one item I call:
Remove-Item '.\Documents\*\Bin\' ,'.\Documents\*\Inter\' -Force -Recurse
I have tried to create the alias like this:
New-Alias -Name 'Clean-RCD' Remove-Item '.\Documents\*\Bin\' ,'.\Documents\*\Inter\' -Force -Recurse
Output:
New-Alias: A positional parameter cannot be found that accepts argument 'System.Object[]'.
Any idea how to define this alias correctly?
Unlike in bash, aliases in PowerShell are strict 1-to-1 command name mappings - no extra parameter arguments allowed.
You'll want to create a function instead:
function Clean-RCD {
Remove-Item -Path '~\Documents\*\Bin', '~\Documents\*\Inter\' -Force -Recurse
}
Use of ~ (which resolves to your home folder) over . is intentional - this way it'll still work if you've navigated to a different path

Win10: Using symbolic links in the PATH variable

Here's the deal. I want to use a symbolic link to the latest installed Java version and create a symbolic link. The PATH variable shall contain the symbolic link as entry. The link is not resolved, though. Why?
Example:
$> dir C:\Program\ Files\Java
C:\Program\ Files\Java\jdk-1.14.1\
C:\Program\ Files\Java\jdk-latest.lnk # link to jdk-1.14.1
$> echo %PATH%
#...
C:\Program\ Files\Java\jdk-latest\bin
$> java --version
The command "java" is either written wrong or couldn't be found.
New-Item cmdlet with -ItemType SymbolicLink can be used to create symbolic link to file or folder.
Suppose, you have 2 folders containing 2 different versions of nodejs
node-v14.18.0-win-x64
node-v14.20.0-win-x64
We need one symbolic link, say rel which will point to one of the nodejs folders.
Add C:\Program\Files\nodejs\rel to PATH environment variable
Point rel to node v14.18.0
New-Item -Type SymbolicLink -Path .\rel -Target .\node-v14.18.0-win-x64 -Force
Verify node version
PS C:\Users\pvaddepa> node -v
v14.18.0
Use -Force to update the symbolic link to point to node v14.20.0.
New-Item -Type SymbolicLink -Path .\rel -Target .\node-v14.20.0-win-x64 -Force
Verify the node version
PS C:\Users\pvaddepa> node -v
v14.20.0
See New-Item example .

How to delete folders that aren't empty?

I am using these lines. Is there any way to shorten it or is this the best?
Objective is to delete the Refresh folder and all of its contents. The script works, but I'd like it to be as minimal as possible.
# Set var $Path to equal Refresh folder
$Path = "C:\data\Refresh"
# Find sub-folders of Refresh
Get-ChildItem -Path $path -Recurse | Remove-Item -Force -Recurse
# Delete Refresh top level folder itself
Remove-Item $Path -Force
Since you want to delete the folder and all its content don't bother with Get-ChildItem. Simply call Remove-Item with the path you want to delete.
$Path = 'C:\data\Refresh'
Remove-Item $Path -Recurse -Force
or just
Remove-Item 'C:\data\Refresh' -Recurse -Force

New-Item recursive registry keys

With PowerShell you can run a command like this
ni c:/foo/bar -type directory
and it will create foo and bar as necessary. However if you run
ni hklm:software/classes/firefoxhtml/shell/edit/command -type directory
all keys but the last must exist or an error will be generated. Can PowerShell generate the parent keys as needed?
I was just missing the -force parameter
New-Item hklm:software/classes/firefoxhtml/shell/edit/command -Force
Using -Force will also remove everything under the key if it already exists so a better option would be
if(!(Test-Path $path)){
New-Item $path -Force;
}

Resources