• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 59
  • Last Modified:

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


--------------------------------------------------------------------------------
0
Melvin Michael
Asked:
Melvin Michael
2 Solutions
 
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
 
QlemoBatchelor, 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
 
QlemoBatchelor, 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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now