SOLVED

Get-ChildItem & Write-Host - output missing

Copper Contributor

I've created a simple script to search for a folder (study) in a list of locations. This has been working ok during testing, but I ocassionally encounter a issue where the output doesn't output the path. For example, it only outputs "Study 12345 found. Location - ", rather than "Study 12345 found. Location - @{FullName=\\file-server\e$\folder1\folder2\folder3\folder4\folder5\12345". This also occurs when searching locations locally on the Server.

 

I can see the folder does exist and it doesn't just happen for one location. I'm puzzled by this and unsure where to start troubleshooting.

 

Any suggestions?

 

 

 

 

# Prompt user for study number
$study = Read-Host -Prompt 'Enter the study number to search for...'

# Create variable with value containing a list of study locations.
$study_paths = Get-Content -Path 'E:\pshell-testing\locations2.txt'

# Define recursion depth
$rec_depth = 3

# Begin searching
foreach ($study_path in $study_paths)
{
    $study_dirs = Get-ChildItem -Directory -Force -ErrorAction SilentlyContinue -Recurse -Depth $rec_depth -Path $study_path | Where-Object {$_.Name -match $study} | Select-Object -Property FullName 
    
    if (!$study_dirs)
    {
        Write-Host -ForegroundColor Yellow "No studies found in $study_path"
    }
    elseif ($study_dirs)
    {
        Write-Host -ForegroundColor Green "Study $study found. Location - $study_dirs"
    }
}

 

 

 

 

4 Replies
best response confirmed by user4444 (Copper Contributor)
Solution

@user4444 

 

You have to enclose the variable and use .fullname behind it, you can see the options if you enter $study_dirs after you run the script

 


 

# Prompt user for study number
$study = Read-Host -Prompt 'Enter the study number to search for...'

# Create variable with value containing a list of study locations.
$study_paths = Get-Content -Path 'E:\pshell-testing\locations2.txt'

# Define recursion depth
$rec_depth = 3

# Begin searching
foreach ($study_path in $study_paths)
{
    $study_dirs = Get-ChildItem -Directory -Force -ErrorAction SilentlyContinue -Recurse -Depth $rec_depth -Path $study_path | Where-Object {$_.Name -match $study} | Select-Object -Property FullName 
    
    if (!$study_dirs)
    {
        Write-Host -ForegroundColor Yellow "No studies found in $study_path"
    }
    elseif ($study_dirs)
    {
        Write-Host -ForegroundColor Green "Study $study found. Location - $($study_dirs.Fullname)"
    }
}
Hello Harm_Veenstra,
thank you for your reply and help.
The amendments you've suggest do improve the script. Where I wasn't receiving the path I now do. However, I now receive it twice? I've tried some variations of your amendment, but I've not been able to get the output to only show the path once.
This isn't big issue, at least I am able to see the path, but it would be good know why I'm seeing it twice?

Example output:
Study 12345 found. Location - \\File-Server1\e$\folder1\F2\f3\12345 \\File-Server1\e$\folder1\F2\f3\12345
Actually, ignore my last reply.

I think I've spotted the issue...its me :) I think there are two folders of the same name being found on the same path, one is in a subdirectory. This is actually better than I expected.

Thank you.
Ah, good to hear :)
1 best response

Accepted Solutions
best response confirmed by user4444 (Copper Contributor)
Solution

@user4444 

 

You have to enclose the variable and use .fullname behind it, you can see the options if you enter $study_dirs after you run the script

 


 

# Prompt user for study number
$study = Read-Host -Prompt 'Enter the study number to search for...'

# Create variable with value containing a list of study locations.
$study_paths = Get-Content -Path 'E:\pshell-testing\locations2.txt'

# Define recursion depth
$rec_depth = 3

# Begin searching
foreach ($study_path in $study_paths)
{
    $study_dirs = Get-ChildItem -Directory -Force -ErrorAction SilentlyContinue -Recurse -Depth $rec_depth -Path $study_path | Where-Object {$_.Name -match $study} | Select-Object -Property FullName 
    
    if (!$study_dirs)
    {
        Write-Host -ForegroundColor Yellow "No studies found in $study_path"
    }
    elseif ($study_dirs)
    {
        Write-Host -ForegroundColor Green "Study $study found. Location - $($study_dirs.Fullname)"
    }
}

View solution in original post