PoshTip #33 – Creating And Managing Symbolic Links

PowerShell 5.0 includes significant new features that extend its use and improve its usability. In this blog post, we will manage symbolic links using PowerShell. Thanks to WMF 5, now it’s pretty easy to create a symbolic link.

New-Item, Remove-Item, and Get-ChildItem have been enhanced to support creating and managing symbolic links. Now, you can create symbolic links in a single line which is a great thing! Use the -ItemType parameter by specifying SymbolicLink as the value of this parameter.

Below is an example. First I will create a simple file:

PS > New-Item -ItemType "File" -Path "C:\Users\Nicolas\Documents" -Name "MyFile.txt" -Value "Welcome"

 Directory: C:\Users\Nicolas\Documents

Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 09.10.2016 17:24 7 MyFile.txt

MyFile.txt is created.

Now, I will create the symbolic link:

PS > New-Item -ItemType "symboliclink" -Target "C:\users\Nicolas\Documents\MyFile.txt" -name "MySymbolicLink"

    Directory: C:\Users\Nicolas

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
-a---l       09.10.2016     17:24              0 MySymbolicLink

Its’ done!

If you want to remove your symbolic link:

PS > Remove-Item -Path "C:\Users\Nicolas\MySymbolicLink"

Now, Get-ChildItem cmdlet can display SymbolicLink file type:

PS > gci

    Directory: C:\users\Nicolas

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-r---       01.10.2016     09:52                Contacts
d-r---       01.10.2016     09:52                Desktop
d-r---       09.10.2016     17:28                Documents
d-r---       01.10.2016     09:52                Videos
-a---l       09.10.2016     17:24              0 MySymbolicLink

 

Thanks for reading! You can follow me on Twitter @PrigentNico

About Nicolas 282 Articles
I work as an IT Production Manager, based in Paris (France) with a primary focus on Microsoft technologies. I have 10 years experience in administering Windows Servers. . I am a Microsoft MVP for Cloud & Datacenter Management. I also received the PowerShell Hero 2016 award by PowerShell.0rg. And finally, I am "MCSE: Cloud Platform and Infrastructure", "MCSA: Windows Servers", "Administering & Deploying SCCM", and CheckPoint CCSA certified.