Windows : navigate into symlink - windows

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

Related

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 .

Find specific .exe files on a computer with PowerShell

So at the company I am currently doing my internship at, they have about 20 'technical laptops' that have company-specified software. They are located in the C:\ directory.
For example:
aaa.exe is located at C:\aaa\aaa.exe
bbb.exe is located at C:\bbb\bbb.exe
What I actually need is a .ps1 script to show these specific executables in a list.
What I have so far is:
Get-ChildItem C:\ -recurse | where {$_.extension -eq ".exe"}
Now, I believe it's just edit the search query because this gives me all the .exe files on the C:\ drive and I just need aaa and bbb.
FYI: I need the script so that I can use it to monitor zabbix agents and see if the specific software is on the computer so I can run this script on the Zabbix Server.
You are looking for the -include parameter:
Get-ChildItem C:\ -recurse -include "aaa.exe", "bbb.exe"
#Martin Brandl has already answered your first question. For the second part of your question, you can do something like this -
$AllDrives = Get-PSDrive -PSProvider 'FileSystem'
foreach ($drive in $AllDrives)
{
Get-ChildItem -path $drive.Root -recurse -include "aaa.exe", "bbb.exe"
}
Or you can do it in one line -
Get-PSDrive -PSProvider 'FileSystem' | % {Get-ChildItem -path $_.Root -recurse -include "aaa.exe", "bbb.exe"}
You can change the -PSProvider parameter if your executables are on other drives other than FileSystem drives.

Powershell Remove Symbolic Link 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"

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