0

I am trying to suppress the output of a command while setting a variable. I need to do this because I have compiled the PowerShell Script using PS2EXE-GUI. This program shows all command output on a seperate window, which is very annoying and is not necessary. I am trying to use Get-ChildItem "$MYSQLFOLDERLOCATION" | Out-String | Write-Host, but this only works when there is nothing in the directory, anything else, and it shows the directory contents on the screen. I have also been trying to use Get-ChildItem "$MYSQLFOLDERLOCATION" | $ISMYSQLFOLDERPRESENT = $? | Out-Null, but this is syntactically incorrect. Is there something similar to this that I can use? I basically want to do an ls-equivalent command in PowerShell, then store whether it succeeded or not, while not showing anything on screen (I can't use Write-Output, Write-Host, etc.)

4
  • 1
    Sounds like you're looking for $IsMySqlFolderPresent = Test-Path $MYSQLFOLDERLOCATION -PathType Container? Commented Oct 21, 2020 at 16:54
  • Precisely! I'm new to PowerShell and never knew this existed :) If you would like to turn that into an answer, I will accept it. Commented Oct 21, 2020 at 16:57
  • Agreed, Get-Childitem is the wrong command here. And using Write-Host exacerbates the problem as it will always output to the console whatever else you do. Test-Path is the way. Commented Oct 21, 2020 at 16:59
  • I am coming from a non-Windows environment (Linux-type) and I always used ls, set variable, clear :) Commented Oct 21, 2020 at 16:59

1 Answer 1

2

You need to assign the entire expression to a variable - otherwise any resulting output will "bubble up" to the caller and you'll see it on the screen:

$variable = Get-ChildItem .\path

For checking whether a folder exists in the file system or not, I'd suggest using Test-Path instead of Get-ChildItem:

$IsMySqlFolderPresent = Test-Path $MYSQLFOLDERLOCATION -PathType Container

$IsMySqlFolderPresent will now hold a boolean value ($true or $false) depending on whether a directory exists at the path specified by $MYSQLFOLDERLOCATION

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

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.