How to create a Hardlink using the New-Hardlink PowerShell PSCX command - powershell

I want to create a new Hardlink with the PowerShell Community Extensions PSCX commandlet New-Hardlink http://pscx.codeplex.com/. I have read the man file and tried almost every combination of commands but it won't work. What am I missing? (I know about fsutil, but I want to use this commandlet/alias)
Here is the directory structure:
E:\Source
E:\Test
Here are some variations of the command that I have tried:
New-Hardlink E:\Test\Source E:\Source
New-Hardlink -Path:"E:\Test\Source" -Target:"E:\Source"
New-Hardlink E:\Source E:\Test\Source
New-Hardlink E:\Source E:\Test\
New-Hardlink -P:"E:\Source" -T:"E:\Test\Source"
Here is the supposed syntax:
New-Hardlink [-Path] <String> [-Target] <String> [<CommonParameters>]
-Path <String>
Path to the new link.
-Target <String>
Target of the link.
The result is always some from of:
New-Hardlink : Unable to find the file 'E:\Source.
Does this command not work with directories but only with files?

I will sheepishly answer my own question.
Yes, indeed Hardlinks refer to files. To accomplish this with directories the New-Junction command should be used like so:
New-Junction E:\Test\Dest E:\Source
The first parameter refers to the location you would like to place the new Junction.
The second parameter refers to the directory you wish to Junction

Powershell 5+ include a native way to create any types of hard-/soft-links and junctions.
For those coming from Google:
PowerShell 5.0 and above have support for creating Symbolic Links and Junctions using the New-Item cmdlet.
In the following, clicking on B.txt will take you to A.txt. Similarly for a directory.
# To create a symbolic link on a file:
New-Item -ItemType SymbolicLink -Name B.txt -Target A.txt
New-Item -ItemType SymbolicLink -Path C:\Temp\B.txt -Value A.txt
# To create a hard-link on a file:
New-Item -ItemType HardLink -Path C:\B.txt -Value C:\A.txt
# To create a symbolic link on a directory:
New-Item -ItemType SymbolicLink -Name B_Directory -Target C:\A_Directory
# To create a junction on a directory:
New-Item -ItemType Junction -Path C:\Junction -Value C:\A_Directory

Related

mkdir vs New-Item , is it the same cmdlets?

I found that there are two different cmdlets : New-Item and mkdir, firstly I was thinking that mkdir is one of aliases of New-Item, but it is not:
Try to get aliases of it, it is md for mkdir and ni for New-Item :
So I am a little bit confused, what the difference between that cmdlets, because powershell reference gives me almost the same pages: mkdir, New-Item
But New-Item is in Microsoft.PowerShell.Management and mkdir in Microsoft.PowerShell.Core , but the do the same(or not?)! Why there are two same cmdlets in powershell?
New-Item is a cmdlet, defined in an assembly, which creates new objects - both files and directories. mkdir is a function which calls New-Item to create directories specifically. It is provided for convenience to shell users who are familiar with Windows CMD or unix shell command mkdir
To see the definition of mkdir use Get-Content Function:\mkdir. You can see that it calls New-Item under the covers, after some parameter and pipeline management. Using PS 5.0:
$wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('New-Item', [System.Management.Automation.CommandTypes]::Cmdlet)
$scriptCmd = {& $wrappedCmd -Type Directory #PSBoundParameters }
Both of the following commands will create a new directory named foo in the root of C:\. The second form is familiar to people coming from other shells (and shorter to type). The first form is idiomatic PowerShell.
PS> New-Item -Path C:\foo -Type Directory
PS> mkdir C:\foo
Because mkdir hardcodes the -Type Directory parameter, it can only be used to create directories. There is no equivalent mkfile built-in function. To create files, use New-Item -Type File, or another cmdlet such as Out-File.

Create directory and file in same command using PowerShell

I try to create a folder and a file in one PowerShell command:
I tried:
New-Item hello\test\ (New-Item hello\test\file.txt -type file) -type directory -Force
and
New-Item file.txt (New-Item hello\test\ -type direcotry) -type file -Force
But both don't seem to work as expected.
What I want to achieve is one command to create hello\test\file.txt
Just provide the filepath you want, including the directory, and it will work:
New-Item -Path '.\hello\test\file.txt' -ItemType File -Force
If you want to put the c.txt file in the b folder of the a folder, do so. ni a/b/c.text -Force
If you want to put two files y.txt and z.hml in x folder ni x/y.txt, x/z.html -Force
I created the directory and the file with the following code.
New-Item hello\test -type Directory ; Start-Sleep -Milliseconds 500 ; New-Item hello\test\file.txt
It's not case-sensitive in powershell command, so the most convenient and fastest way to create file would be - > new-item -path . -name 'newfile.txt' -itemtype 'file'
And for new directory, it's the same as in Linux-terminal -> mkdir newdirectory

Piping out to "New-Items" in PowerShell

I'm trying to output to a file that is created on the fly, but I can't seem to get either to work. Here's that portion of my code-
New-Item -Path $LogPath -Name $InfoLog -Type File -Force
New-Item -Path $LogPath -Name $ErrorLog -Type File -Force
"Script started at: $DateStamp_${TimeStamp}" | $InfoLog
I've also tried just ">>" instead of the pipe. The script runs fine, it just doesn't pipe the output into the file. Instead it pipes it out to a a files called "0" in the directory the script ran from.
Three things.
First, New-Item outputs the item it creates. So unless you do something with the output objects, New-Item will output the new file items to the pipeline. So I think you might want to say:
New-Item -Path $LogPath -Name $InfoLog -Type File -Force | Out-Null
Second, since you're not specifying the path to the file you want to write to, PowerShell will assume the current location.
Finally, if you want to write output to a log file, you probably want to use Out-File. Perhaps something like this:
$infoLogPath = Join-Path $LogPath $InfoLog
"Script started at: $DateStamp_${TimeStamp}" | Out-File $infoLogPath
Join-Path combines the directory and filename into a fully-qualified path name.

Combine Two Commands in PowerShell

I know there has to be a simple answer but how can I run both my commands at the same time? I can run one line to create the new directory and name it, then copy items from one folder to another. But how do I create the folder and copy the files into that created folder?
#Creates new directory and names it current date and time.
New-Item C:\ -type directory -Name ("$(Get-Date -f ddMMyyyy_hhmm)")
#Copies Data from one directory to another, works good.
Copy-Item C:\test\* C:\test2
Thanks for your help.
You could use the FullName property of the object that New-Item returns as the destination for another command. i.e.
Copy-Item C:\somefolder\* -Destination (New-Item -type Directory -name (Get-Date -f ddMMyyyy_hhmmss)).FullName

Copy a single file to a non-existing folder

It's easy to copy multiple files to a folder that doesn't exists and let it be created:
Copy-Item C:\Temp\aa C:\Temp2\DoesNotExist
The command above will create the folder DoesNotExist. That's what I'm after.
But what is the PowerShell syntax to the same when the source is only a single file?
Copy-Item C:\Temp\test.txt C:\Temp2\DoesNotExist
I tried C:\Temp2\DoesNotExist\ (with the trailing slash), but Powershell says "The filename, directory name, or volume label syntax is incorrect." and refuses to copy the single file.
If you're looking for a one liner solution, you can do this.
copy "C:\test2.txt" -Destination (New-Item "C:\Temp2\DoesNotExist\" -Type container -force) -Container -force
I think Geoff Guynn's one-liner should be as follows:
Copy-Item -Path "C:\test2.txt" -Destination (New-Item "C:\Temp2\DoesNotExist\" -ItemType directory -Force) -Force
The parameter for the cmdlet New-Item should be -ItemType and the intended "Type" should be directory.
The additional parameter -Container for the cmdlet Copy-Item seems to me superfluous; on the one hand it is set to $true by default anyway, on the other hand a single file should be copied and not the folder structure should be preserved.