resolver101
resolver101

Reputation: 2255

How do i join custom object to a string

I want to test paths exist. I've written the following script but i cant add string to custom object. This is the offending line $DesktopCheck = | $FName + "\desktop". How do i add the string "desktop" to custom object $fname?

$FoldNames = dir \\server\RedirectedFolders | Select-Object fullname 

foreach ($FName  in $FoldNames)
{
     $DesktopCheck = | $FName + "\desktop"
     Test-Path $DesktopCheck 
}

Thanks

Upvotes: 1

Views: 62

Answers (2)

mjolinor
mjolinor

Reputation: 68331

Try this:

$FoldNames = dir \\server\RedirectedFolders | Select-Object -ExpandProperty fullname 

foreach ($FName  in $FoldNames)
  {
     Join-Path -Path $FName -ChildPath '\desktop'
     Test-Path $DesktopCheck 
  }

Upvotes: 0

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200473

Use Join-Path for constructing paths. It helps avoiding headaches caused by having to keep track of leading/trailing backslashes. I would also recommend using the option -LiteralPath unless you need wildcards/patterns in the match. Try this:

dir \\server\RedirectedFolders | % {
  Test-Path -LiteralPath (Join-Path $_.FullName "desktop")
}

Upvotes: 4

Related Questions