J. Doe
J. Doe

Reputation: 1317

Hide powershell output

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)

Upvotes: 77

Views: 94013

Answers (1)

ChiliYago
ChiliYago

Reputation: 12279

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?

Upvotes: 129

Related Questions