15

i am comparing to folders with all their subfolders with powershell and its working fine on my local machine. but when i tried it on server its give me error and append

Microsoft.PowerShell.Core\FileSystem::\\

to all the files

if any one have do such work earlier please help

my script is

$Path = '\\Server1\Folder1'

Get-ChildItem $Path -Recurse | ? { !$_.PSIsContainer } | % { 
        Add-Member -InputObject $_ -MemberType NoteProperty -Name RelativePath -Value $_.FullName.Substring($Path)
        $_
    }

and its give me error

Cannot convert argument "0", with value: "Microsoft.PowerShell.Core\FileSystem::\\Server1\Folder1\ServerListPowershell", for "Substring" to type "System.Int32": "Cannot convert value "M
icrosoft.PowerShell.Core\FileSystem::\\Server1\Folder1\ServerListPowershell" to type "System.Int32". Error: "Input string was not in a correct format.""
At C:\Users\cwr.satish.kumar\AppData\Local\Temp\898f72f1-a0d3-4003-b268-128c5efc9f2b.ps1:14 char:108
+         Add-Member -InputObject $_ -MemberType NoteProperty -Name RelativePath -Value $_.FullName.Substring <<<< ($Path)
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument

please help

CB.
  • 58,865
  • 9
  • 159
  • 159
satish kumar
  • 443
  • 1
  • 3
  • 10

3 Answers3

31

Try the Convert-Path cmdlet:

PS> Convert-Path Microsoft.PowerShell.Core\FileSystem::C:\windows\system32
C:\windows\system32
Shay Levy
  • 121,444
  • 32
  • 184
  • 206
  • 3
    NB: This requires that the path exist on the machine on which it's executed (so if the path were returned from `Invoke-Command` on a remote machine, then `Convert-Path` were run against the results locally, you'd get an error). To avoid such issues you can use: `$ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath` (see https://stackoverflow.com/a/3040982/361842) – JohnLBevan Aug 19 '19 at 12:34
3

I am not sure why the FullName property is prepending the powershell provider to the name, that's usually what you get when in the PsPath property.

At any rate, the reason why your attempt to strip it is failing is because your are passing a String to the SubString member function when it expects an integer index. To get the index of the start of your path, use the IndexOf member function:

$justThePath = $_.FullName.SubString($_.FullName.IndexOf($Path))
zdan
  • 28,667
  • 7
  • 60
  • 71
  • Now script give me other error Method invocation failed because [System.IO.FileInfo] doesn't contain a method named 'IndexOf'. At \AppData\Local\Temp\78c0b727-7a26-4b1f-8250-43262dbb326e.ps1:14 char:110 + Add-Member -InputObject $_ -MemberType NoteProperty -Name RelativePath -Value $_.SubString($_.IndexOf <<<< ($Path))#$_.FullName.Substring($Path) + CategoryInfo : InvalidOperation: (IndexOf:String) [], RuntimeException + FullyQualifiedErrorId : MethodNotFound – satish kumar Feb 01 '13 at 18:57
  • @satishkumar oops, I forgot the `fullname` property. See my updated answer. – zdan Feb 01 '13 at 19:03
  • Again the same error `Directory: \\Server1\ServerListPowershell\PowerScript Mode LastWriteTime Length Name ---- ------------- ------ ---- -ar- 10/9/2012 12:03 PM 1473 01 Server.ps1 Exception calling "Substring" with "1" argument(s): "StartIndex cannot be less than zero. Parameter name: startIndex" At AppData\Local\Temp\78c0b727.ps1:16 char:108 + Add-Member -InputObject $_ -MemberType NoteProperty -Name RelativePath -Value $_.FullName.SubString <<<< ($_.FullName.IndexOf($Path)) + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedEr` – satish kumar Feb 01 '13 at 19:11
  • @satishkumar No, that's not the same error, you're getting "StartIndex cannot be less than zero" which means that IndexOf() can't find $Path in the string. You have something else going on. What is the value of $_ and $Path when it fails? – zdan Feb 01 '13 at 20:15
0

You could use the Substring() as well. For example:

$path = "Microsoft.PowerShell.Core\FileSystem::\\SERVERNAME\HOSTNAME\FOLDER"
$result = $path.Substring($path.IndexOf("FileSystem::") + 12)
Write-Output $result # Output: "\\SERVERNAME\HOSTNAME\FOLDER"