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.)
1 Answer
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
$IsMySqlFolderPresent = Test-Path $MYSQLFOLDERLOCATION -PathType Container?