0

So I'm working on a script that will have a variable set to a file path, then it will Get-ChildItem of that path and if these items are over a certain size then it will print the output of that with the name of the file and the file size.

Get-ChildItem $file | ? {$_.Length -gt 1mb} | ForEach-Object {Write-Host "Users:" $_.name "have Outlook Data Files larger than 8gb, with a total of" ("{0:N2}" -f($_.length/1mb)) "mb"}

I am trying to assign this output, to a variable so I can utilize the second command and send this output in an email to myself. Unless there is a better way to accomplish this.

5
  • 1
    We need to know what's not working... Where's the rest of your code? Help us help you. Commented Dec 21, 2018 at 18:01
  • Write-Host results go directly to a display. The correct cmdlet to use is Write-Output. Commented Dec 21, 2018 at 18:38
  • @rpm192 It's not that it's not working, I just am unsure how how to set the output of the above line of code to a variable so I can call it in a Send-MailMessage on the next line. Commented Dec 21, 2018 at 19:20
  • @LoganGregory - as JosefZ pointed out ... you are sending things to the screen. if you want to capture the current screen output, remove the Write-Host stuff and let things go to the output stream. THEN add a $VarName = in front of your line of code. Commented Dec 21, 2018 at 19:49
  • I guess I'm not quite understanding because when I use ? {Write-Output $final = "Users:" ... it seems to work but when I try and call that variable it just outputs a return. Sorry guys, it's my second week with powershell. And I appreciate the replies. Commented Dec 21, 2018 at 20:39

2 Answers 2

1
$content = gci -Recurse -File | ? { $_.Length -gt 40000 }

Include $content as the body of your email.

Sign up to request clarification or add additional context in comments.

Comments

0

As the previous comments have pointed out, it seems like your primary problem might be a misunderstanding about how the Write-* cmdlets work in PowerShell. Write-Host outputs directly to the host/console, bypassing the normal PowerShell output streams. This can be illustrated quickly by running the following commands in a PowerShell session:

$MyVariable1 = Write-Host "Hello, World!"
$MyVariable2 = Write-Output "Hello, World!"
$MyVariable1
$MyVariable2

If you run the above, you'll find that $MyVariable1 has no value assigned (and you could actually test it for that with something like $null -eq $MyVariable1) but $MyVariable2 will have the value 'Hello, World!'.

For your example to work to get your output into a variable you would need to run something like the following:

$LargeFiles = Get-ChildItem $file | ? {$_.Length -gt 1mb} | ForEach-Object {Write-Output "Users:" $_.name "have Outlook Data Files larger than 8gb, with a total of" ("{0:N2}" -f($_.length/1mb)) "mb"}

To learn more about PowerShell output streams you might also want to read the about_redirection article.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.