Creating SymbolicLink with PowerShell does not work - powershell

I'm running my PowerShell as Admin and I try to create a symbolic link to another directory.
To do so, I want to use the New-Item cmdlet as described in the
Microsoft documentation.
New-Item -ItemType SymbolicLink -Path C:\Temp -Name TestDir -Value C:\LinkedDir
I made sure, all directories (except the symbolic link itself) exist, but still I get this error:
New-Item : Type unkonwn Typ. Only "file" and "directory" can be used.
In Line:1 Row:9
+ New-Item <<<< -ItemType SymbolicLink -Path C:\Temp -Name TestDir -Value C:\LinkedDir
+ CategoryInfo : InvalidArgument: (:) [New-Item], PSArgumentException
+ FullyQualifiedErrorId : Argument,Microsoft.PowerShell.Commands.NewItemCommand
If I use the New-Item cmdlet just like this
PS C:\Temp> New-Item -ItemType SymbolicLink
I can enter the paths manually, so this means, that my PowerShell actually knows the cmdlet.
Does anyone know what the problem is and can help me out?

The code above does not work with PowerShell 2.0 .
After Mark Wragg asked which PS version I use, I upgraded to 4.0 and it worked.
So it seems, that some parameters from the New-Item cmdlet are not supported in PS 2.0.

New-Item SymbolicLink error Workaround:
On my computer, this command does't work:
New-Item -ItemType SymbolicLink -Path $link_fullpath -Target $Target
If $link and $target are on the same unit, for example, if $Target= "y:\xxx\myfile1" and $link= "y:\zzz\Myfile2" (both on Y:). If it's not on the same unit, it works.
On the other hand and fortunately, New-Item works if I use -Name like this:
New-Item -ItemType SymbolicLink -name $name_link -Path $path_link -Target $target
Where $name_link = "Myfile2" , $path_link = "y:\zzz" and $Target = "y:\xxx\myfile1"
So, use -Name in New-Item SymbolicLink if you have the same error.

Related

Add trusted File Share to internet explorer

I need a bit of help, I need to add my File Share \169.254.100.100\Share to the trusted Sites in IE, I did it the manual way, so I know that is working. But I'd like to do it also in PS. I tried adding it in the Registry and GPO in different ways but failed. For websites it works but not for the Server.. any suggestions?
New-Item -Path '.\Software\Policies\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap' -Name \\169.254.100.100\Share
New-ItemProperty -Path '.\Software\Policies\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains' -Name\\169.254.100.100\Share -Value 1 -PropertyType Dword
But this fails which is ok but I don't have other ideas.
BR Tim
If you're adding an IP Address it goes in the Ranges subkey not the Domains subkey, you also need to specify the file URL scheme and populate the ZoneMapKey. The parent subkeys may not exist, on my test PC even ZoneMap was not present. The example below should be expanded to test what exists and what needs creating.
Set-Location "HKCU:"
New-Item -Path '\Software\Policies\Microsoft\Windows\CurrentVersion\Internet Settings\' -name ZoneMap
New-Item -Path '\Software\Policies\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap' -name Domains
New-Item -Path '\Software\Policies\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap' -name Ranges
New-Item -Path '\Software\Policies\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Ranges' -name Range1
New-ItemProperty -Path '\Software\Policies\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Ranges\Range1' -Name ":Range" -Value "169.254.100.100"
New-ItemProperty -Path '\Software\Policies\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Ranges\Range1' -Name "file" -Value 2
New-Item -Path '\Software\Policies\Microsoft\Windows\CurrentVersion\Internet Settings\' -name ZoneMapKey
New-ItemProperty -Path '\Software\Policies\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMapKey' -Name "file://169.254.100.100" -Value "2"
You'd need to make sure Range1 doesn't exist and find RangeN where N doesn't exist.

Creating Symbolic link to UNC path is not working?

I'm having problem getting symbolic links to work in powershell as I can't create the folder on the network drive. I've tried Test-NetConnection -ComputerName crex2cloud -Port 445 I get a response but when I try to create the symbolic link with the following command; New-Item -ItemType SymbolicLink -path $pathh -Target $target it doesn't work.
$pathh variable is the source and $target is the target folder which I want to create, although each time I attempt to create a symbolic link using the above command I get this error;
New-Item : Cannot find path 'T:\csync' because it does not exist.
At line:1 char:1
+ New-Item -ItemType SymbolicLink -path $pathh -Target $target
When I try using the UNC path, first there is a pause; then this error;
New-Item : Cannot find path '\\crex2cloud\csync' because it does not exist.
At line:1 char:1
+ New-Item -ItemType SymbolicLink -path $pathh -Target $target
Use Test-Path instead of Test-NetConnection
Then you can also wrap it in an if statement:
if (Test-path '\\crex2cloud\csync') {Write-host "path found"}
else
{New-Item -ItemType SymbolicLink -path $pathh -Target $target}
Also double check your permissions on the share. You can use the admin share also.
"\\server\c$\whateverfolder"

Hide powershell output

I have the following script:
param([Parameter(Mandatory=$true)][string]$dest)
New-Item -force -path "$dest\1\" -itemtype directory
New-Item -force -path "$dest\2\" -itemtype directory
New-Item -force -path "$dest\3\" -itemtype directory
Copy-Item -path "C:\Development\1\bin\Debug\*" -destination "$dest\1\" -container -recurse -force
Copy-Item -path "C:\Development\2\bin\Debug\*" -destination "$dest\2\" -container -recurse -force
Copy-Item -path "C:\Development\3\bin\Debug\*" -destination "$dest\3\" -container -recurse -force
The script takes a string and copies all files and folders from the static origin path to the given root string, amending some folders for structure clarity.
It works fine but prints out the results from the "New-Item" commands and I would like to hide that. I've looked at the net and other questions on SE but no definitive answers to my problem were found.
In case someone is wondering - I am using "New-item" at the beginning in order to circumvent a flaw in PS' -recurse parameter not copying all subfolders correctly if the destination folder does not exist. (I.e. they are mandatory)
Option 1: Pipe it to Out-Null
New-Item -Path c:\temp\foo -ItemType Directory | Out-Null
Test-Path c:\temp\foo
Option 2: assign to $null (faster than option 1)
$null = New-Item -Path c:\temp\foo -ItemType Directory
Test-Path c:\temp\foo
Option 3: cast to [void] (also faster than option 1)
[void](New-Item -Path c:\temp\foo -ItemType Directory)
Test-Path c:\temp\foo
See also: What's the better (cleaner) way to ignore output in PowerShell?

How to create symbolic link for non-existing folder?

I'd like to create link to a folder on desktop of remote computer. I do not have permissions to execute scripts on that computer, but I can copy files to that computer.
My idea was to create link to folder on local computer and then copy the link to remote computer.
But, I am getting error New-Item : Cannot find path 'C:\SomeFolder' because it
does not exist.
Here is my command:
New-Item -Path "c:\Users\pocherka\Desktop\link" -ItemType SymbolicLink -Value "c:\SomeFolder" -Force
Any ideas for workaround?
Try adding the -force parameter:
New-Item -Path "c:\Users\pocherka\Desktop\link" -ItemType SymbolicLink -Value "c:\SomeFolder" -force
You can do using mklink also . Make sure that the destination folder is available . You can use the Test-Path to check that :
$destination = "c:\SomeFolder"
if(Test-Path $destination)
{
cmd /c mklink "c:\Users\pocherka\Desktop\link" $destination
# OR you can use the new-item also. Just commented in the below line
# New-Item -Path "c:\Users\pocherka\Desktop\link" -ItemType SymbolicLink -Value $destination
}
else
{
New-Item $destination -ItemType Directory -Force
cmd /c mklink "c:\Users\pocherka\Desktop\link" $destination
}
Hope it helps

Powershell - NewItemUnauthorizedAccessError

I receive the following error when using the New-Item cmdlet to create backup copies of files:
new-item : Access to the path 'C:\Program Files (x86)\PRTG Network Monitor\webroot\mailtemplates' is denied.
At line:1 char:21
+ foreach ($i in $a) {new-item -itemtype file -name $i.bak}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (C:\Program File...t\mailtemplates:String) [New-Item], UnauthorizedAcc
essException
+ FullyQualifiedErrorId : NewItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.NewItemCommand
Here is the command I am executing:
pwd
C:\Program Files (x86)\PRTG Network Monitor\webroot\mailtemplates
$a = ls
foreach ($i in $a) {new-item -itemtype file -name $i.bak}
I am logged on as the local Administrator
Administrator is member of Administrators Group
Administrators Group has full permissions on that folder
I started the POSH console as Administrator
Execution Policy is set to remote signed (just in case it matters)
I can use the New-Item cmdlet to create a test.txt file within that folder
I can use the GUI to create copies of all files within that directory
Not sure where to go from here.
try this:
foreach ($i in $a) {new-item -itemtype file -name "$($i.basename).bak"}