Forum Discussion

user4444's avatar
user4444
Copper Contributor
Jan 26, 2022
Solved

Get-ChildItem & Write-Host - output missing

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 p...
  • Harm_Veenstra's avatar
    Jan 26, 2022

    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)"
        }
    }

Resources