out-null not supressing the output of new-item

I am trying to create  a script that could help me create many folders. No problems with that, but I wanted to suppress the output of new-item . If I use  | out-null on a PS Prompt I see the Output remains suppressed, but when I use it in my script it does not suppress the output. Any idea?

-------------------------------------

Write-Host  "*** Welcome ***"
$path = get-location
$a = Read-Host "Enter the Number for Iteration 1"
$b = Read-Host "Enter the Number for Iteration 2"
for ($i=0;$i -lt $a ; $i++) { $i1 = $i+1 ; $pfname = "AAA$i1" ; Write-Host "Creating Parent Level Folder $pfname " ; New-Item -type Directory -path $path -name $pfname  | out-null ; for ($j=0 ; $j -lt $b ; $j++) { $childpath = "$path\$pfname"  ; $j1 = $j +1; $cfname = "BBB$j1" ; Write-Host "Creating Child Level Folder $cfname" ;  new-item -type Directory -path $childpath -name $cfname | out-null }}


--------------------------------------------------------------------------------
Melvin MichaelAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

footechCommented:
I don't see any output when I run your code (except the expected Read-Host prompts and Write-Host output).  No difference whether it's saved as a script file or not.

Try running your code in a new PS session.
Other than that I would re-examine the code you've inserted into your script to make sure there's no difference.
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
It certainly helps if you use line breaks where they are due, instead of ; and keeping everything in a single line. Your way it is difficult to spot potential issues.
0
Melvin MichaelAuthor Commented:
Thanks Qlemo & Footech, the same piece of script now works fine . Not sure what went wrong earlier. But thanks again.
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
You probably missed something you now have changed unintentionally ;-).
Here is your script "properly" formatted:
Write-Host  "*** Welcome ***"
$path = get-location
$a = Read-Host "Enter the Number for Iteration 1"
$b = Read-Host "Enter the Number for Iteration 2" 
for ($i=0;$i -lt $a ; $i++)
{
  $i1 = $i+1
  $pfname = "AAA$i1"
  Write-Host "Creating Parent Level Folder $pfname "
  New-Item -type Directory -path $path -name $pfname | out-null
  for ($j=0 ; $j -lt $b ; $j++)
  {
    $childpath = "$path\$pfname"
    $j1 = $j +1
    $cfname = "BBB$j1"
    Write-Host "Creating Child Level Folder $cfname"
    New-Item -type Directory -path $childpath -name $cfname | out-null
  }
}

Open in new window

This is improved code, getting rid of $i1 and $j1, as their use makes no sense for the scope of that script. I've also switched to use [void] to supress unwanted output:
Write-Host  "*** Welcome ***"
$path = get-location
$a = Read-Host "Enter the Number for Iteration 1"
$b = Read-Host "Enter the Number for Iteration 2" 
for ($i=1; $i -le $a ; $i++)
{
  $pfname = "AAA$i"
  Write-Host "Creating Parent Level Folder $pfname "
  [void] New-Item -type Directory -path $path -name $pfname
  for ($j=1 ; $j -le $b ; $j++)
  {
    $childpath = "$path\$pfname"
    $cfname = "BBB$j"
    Write-Host "Creating Child Level Folder $cfname"
    [void] New-Item -type Directory -path $childpath -name $cfname
  }
}

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PberSolutions ArchitectCommented:
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I have recommended this question be closed as follows:

Split:
-- Qlemo (https:#a42376685)
-- footech (https:#a42373574)


If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.

Pber
Experts-Exchange Cleanup Volunteer
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.