Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide powershell output

Tags:

powershell

I have the following script:

param([Parameter(Mandatory=$true)][string]$dest)

New-Item -force -path "$dest\1\" -itemtype directory New-Item -force -path "$dest\2\" -itemtype directory New-Item -force -path "$dest\3\" -itemtype directory  Copy-Item -path "C:\Development\1\bin\Debug\*" -destination "$dest\1\" -container -recurse -force Copy-Item -path "C:\Development\2\bin\Debug\*" -destination "$dest\2\" -container -recurse -force Copy-Item -path "C:\Development\3\bin\Debug\*" -destination "$dest\3\" -container -recurse -force 

The script takes a string and copies all files and folders from the static origin path to the given root string, amending some folders for structure clarity.

It works fine but prints out the results from the "New-Item" commands and I would like to hide that. I've looked at the net and other questions on SE but no definitive answers to my problem were found.

In case someone is wondering - I am using "New-item" at the beginning in order to circumvent a flaw in PS' -recurse parameter not copying all subfolders correctly if the destination folder does not exist. (I.e. they are mandatory)

like image 759
J. Doe Avatar asked Oct 05 '17 13:10

J. Doe


People also ask

How do I hide a PowerShell window in a script?

I found out if you go to the Task in Task Scheduler that is running the powershell.exe script, you can click "Run whether user is logged on or not" and that will never show the powershell window when the task runs.

What does out-null do PowerShell?

The Out-Null cmdlet sends its output to NULL, in effect, removing it from the pipeline and preventing the output to be displayed at the screen.

What is SilentlyContinue in PowerShell?

-ErrorAction:SilentlyContinue suppresses the error message and continues executing the command. -ErrorAction:Stop displays the error message and stops executing the command. -ErrorAction:Suspend is only available for workflows which aren't supported in PowerShell 6 and beyond.


1 Answers

Option 1: Pipe it to Out-Null

New-Item -Path c:\temp\foo -ItemType Directory | Out-Null Test-Path c:\temp\foo 

Option 2: assign to $null (faster than option 1)

$null = New-Item -Path c:\temp\foo -ItemType Directory Test-Path c:\temp\foo 

Option 3: cast to [void] (also faster than option 1)

[void](New-Item -Path c:\temp\foo -ItemType Directory) Test-Path c:\temp\foo 

See also: What's the better (cleaner) way to ignore output in PowerShell?

like image 97
ChiliYago Avatar answered Oct 02 '22 18:10

ChiliYago