Set-Content on a not-yet existant file uses incorrect path when a PSDrive is used
Votes from Connect: 3
Original Date Submitted: 8/13/2014 9:25:01 AM
Site Name: PowerShell
Feedback ID: 944947
Frequency: Always Happens
Regression: I do not know if this issue existed previously
If Set-Content is used to create a new file on a PSDrive mapped to a network share (UNC path), then it'll fail because it resolves to a completely incorrect/strange literal path (that contains the root UNC path repeated multiple times).
For example, after following the reproduction steps below,
PS C:\Scripts>Set-Content "Z:\blah.txt" "blah"
Set-Content : Could not find a part of the path '\192.168.111.160\pub\192.168.111.160\pub\192.168.111.160\pub\blah.txt'.
I've seen this happen in PowerShell 4.0, but have not tested previous versions.
One work around is to create the file first, e.g. by using echo "" >> "thefile.txt"
The other is to use the -Persist option, but this undesirable.
Product Studio item created by Connect Synchronizer due to creation of feedback ID 944947 (http://connect.microsoft.com/PowerShell/feedback/ViewFeedback.aspx?FeedbackID=944947).
Assuming a network share at \192.168.111.160\pub requiring credentials username="user" and password="password"
$securePassword = ConvertTo-SecureString 'password' -AsPlainText -Force
$credentials = New-Object PSCredential 'user', $securePassword
$networkPath = "\\192.168.111.160\pub\"
$psDrive = New-PSDrive -Scope Script -PSProvider FileSystem -Name "Z" -Root $networkPath -Credential $credentials
Set-Content "Z:\test.txt" "testing"
A file at \192.168.0.111\public\test.txt is created containing the text "testing"
Internal BugId: 8938
James N commented
Bug still present 4 years later.