Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6239461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:23:41+00:00 2026-05-24T11:23:41+00:00

whenever I run this below it gives the output but it also gives an

  • 0

whenever I run this below it gives the output but it also gives an error if I did not enter in a direcotry. Any idea how I can “hide” the error message that comes?

Checking Directory Count
Read-Host : An error of type “System.Management.Automation.Host.PromptingException” has occurred.
At C:\Users\lara\AppData\Local\Temp\5820c1b7-ee6c-47f1-9a6c-06de6dc9e68f.ps1:2 char:20
+ $Source = Read-Host <<<< “Please enter first directory to check”
+ CategoryInfo : ResourceUnavailable: (:) [Read-Host], PromptingException
+ FullyQualifiedErrorId : System.Management.Automation.Host.PromptingException,Microsoft.PowerShell.Commands.ReadHostCommand

Write-Host "Checking Directory Count" -ErrorAction SilentlyContinue
$Source = Read-Host "Please enter first directory to check" 

If($Source)
{
    Write-host "There are " (Get-ChildItem $Source).Count "items in the ""$Source"" directory"  
}
Else
{
    Write-Host "Please enter a directory"
}

Update:

Thanks for all the help. I basically want to merge what I have below into another script that does folder comparision. I need to know how many files are in the directory I want to check and also the files that do not exist. I have the below code – I know it is really badly written! I just was not too sure how to get the $source and $target into the same IF statment so ended up making a basic mistake of having two IF statements!

Also – is there a way of instead of showing => and <= that I show “does not exist in $source” or “does not exists in $target” – I will be sending this code to another team and do not want to confuse them too much:

Write-Host "Checking Directory Count and Folder comparision" -ErrorAction SilentlyContinue

$Source = Read-Host "Please enter Source directory to check" 
$Target = Read-Host "Please enter Target directory to check"

$child1 = Get-ChildItem -Path $Source
$child2 = Get-ChildItem -Path $Target

Compare-Object $child1 -DifferenceObject $child2 

Write-Host ""
If($source -and (Test-Path -Path $source -PathType Container))
{
    "There are $(@(Get-ChildItem $Source).Count) items in the '$Source' directory"  
}
Else
{
    Write-Host "Please enter a directory"
}
If($source -and (Test-Path -Path $Target -PathType Container))
{
    "There are $(@(Get-ChildItem $Target).Count) items in the '$Target' directory"  
}
Else
{
    Write-Host "Please enter a directory"
}


Write-Host ""
Write-Host "Any symbols with '<=' mean that the file Does NOT exist in TARGET"
Write-Host "Any symbols with '=>' mean that the file Does NOT exist in SOURCE"
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T11:23:42+00:00Added an answer on May 24, 2026 at 11:23 am

    I suggest you first test if the entered path exists before you try to list its content. I would also cast the result to an array, if the result of use Get-ChildItem is a scalar (one object only) it won’t have a count property :

    $Source = Read-Host "Please enter first directory to check" 
    
    If($source -and (Test-Path -Path $source -PathType Container))
    {
        "There are $(@(Get-ChildItem $Source).Count) items in the '$Source' directory"  
    }
    Else
    {
        Write-Host "Please enter a directory"
    }
    

    UPADTE:

    I didn’t test the code but you need to reorder the commands. Also, do the compare on the Name property:

    $Source = Read-Host "Please enter Source directory to check" 
    $Target = Read-Host "Please enter Target directory to check"
    
    If($source -and (Test-Path -Path $source -PathType Container))
    {
        "There are $(@(Get-ChildItem $Source).Count) items in the '$Source' directory"  
    }
    Else
    {
        Write-Host "Please enter a source directory"
        return
    }
    
    If($source -and (Test-Path -Path $Target -PathType Container))
    {
        "There are $(@(Get-ChildItem $Target).Count) items in the '$Target' directory"  
    }
    Else
    {
        Write-Host "Please enter a Target directory"
        return
    }
    
    
    $child1 = Get-ChildItem -Path $Source
    $child2 = Get-ChildItem -Path $Target
    
    Compare-Object $child1 -DifferenceObject $child2 -Property Name
    
    Write-Host ""
    Write-Host "Any symbols with '<=' mean that the file Does NOT exist in TARGET"
    Write-Host "Any symbols with '=>' mean that the file Does NOT exist in SOURCE"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I keep getting this whenever I run the below code: Fatal error: Call to
I tried to install python below way. But this did not work. This take
Whenever i try to run sqlmetal, i get this: 'sqlmetal' is not recognized as
G'day folks I keep getting the error above whenever I run this and buggered
I get this error whenever i run the program Assignment makes pointer from Integer
I am facing this issue whenever I am trying to run Play 1.2.1 in
Thanks in advance. This is my sample code. Whenever i try to run it
I have created an applet within a web page, but whenever I run it,
I am trying to run an andriod application from emulator , but whenever i
I am plannning to use whenever gem which among other things will also run

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.