SOLVED

Get-ChildItem & Write-Host - output missing

%3CLINGO-SUB%20id%3D%22lingo-sub-3072630%22%20slang%3D%22en-US%22%3EGet-ChildItem%20%26amp%3B%20Write-Host%20-%20output%20missing%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3072630%22%20slang%3D%22en-US%22%3E%3CP%3EI've%20created%20a%20simple%20script%20to%20search%20for%20a%20folder%20(study)%20in%20a%20list%20of%20locations.%20This%20has%20been%20working%20ok%20during%20testing%2C%20but%20I%20ocassionally%20encounter%20a%20issue%20where%20the%20output%20doesn't%20output%20the%20path.%20For%20example%2C%20it%20only%20outputs%20%22Study%2012345%20found.%20Location%20-%20%22%2C%20rather%20than%20%22Study%2012345%20found.%20Location%20-%20%40%7BFullName%3D%5C%5Cfile-server%5Ce%24%5Cfolder1%5Cfolder2%5Cfolder3%5Cfolder4%5Cfolder5%5C12345%22.%20This%20also%20occurs%20when%20searching%20locations%20locally%20on%20the%20Server.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20can%20see%20the%20folder%20does%20exist%20and%20it%20doesn't%20just%20happen%20for%20one%20location.%20I'm%20puzzled%20by%20this%20and%20unsure%20where%20to%20start%20troubleshooting.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAny%20suggestions%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%23%20Prompt%20user%20for%20study%20number%0A%24study%20%3D%20Read-Host%20-Prompt%20'Enter%20the%20study%20number%20to%20search%20for...'%0A%0A%23%20Create%20variable%20with%20value%20containing%20a%20list%20of%20study%20locations.%0A%24study_paths%20%3D%20Get-Content%20-Path%20'E%3A%5Cpshell-testing%5Clocations2.txt'%0A%0A%23%20Define%20recursion%20depth%0A%24rec_depth%20%3D%203%0A%0A%23%20Begin%20searching%0Aforeach%20(%24study_path%20in%20%24study_paths)%0A%7B%0A%20%20%20%20%24study_dirs%20%3D%20Get-ChildItem%20-Directory%20-Force%20-ErrorAction%20SilentlyContinue%20-Recurse%20-Depth%20%24rec_depth%20-Path%20%24study_path%20%7C%20Where-Object%20%7B%24_.Name%20-match%20%24study%7D%20%7C%20Select-Object%20-Property%20FullName%20%0A%20%20%20%20%0A%20%20%20%20if%20(!%24study_dirs)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20Write-Host%20-ForegroundColor%20Yellow%20%22No%20studies%20found%20in%20%24study_path%22%0A%20%20%20%20%7D%0A%20%20%20%20elseif%20(%24study_dirs)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20Write-Host%20-ForegroundColor%20Green%20%22Study%20%24study%20found.%20Location%20-%20%24study_dirs%22%0A%20%20%20%20%7D%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3072630%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Occasional 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 (Occasional 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 :)