PowerShell: How to Use New-Item and Hide Output


By default, when you use the New-Item cmdlet in PowerShell to create a new file or folder, the resulting file or folder will be shown in the output along with the date and time that it was created.

There are two ways to use the New-Item cmdlet to create a new item while hiding this output:

Method 1: Use $null

$null = New-Item -Path c:\users\bobbi\data1\south_data -ItemType Directory

Method 2: Use [void]

[void](New-Item -Path c:\users\bobbi\data1\south_data -ItemType Directory)

Both of these methods will create a new folder named south_data in the path that we specified while hiding the output.

The following example shows how to use these methods in practice.

Example: How to Use New-Item and Hide Output in PowerShell

Suppose that our current directory has the following path:

  • c:\users\bobbi\data1

We can use the Dir command to list out all files in this current directory:

Suppose that we would like to create a new folder in this directory named south_data.

Suppose we use the following syntax with the New-Item cmdlet to do so:

New-Item -Path c:\users\bobbi\data1\south_data -ItemType Directory

The following screenshot shows how to use this syntax in practice:

By default, this new folder is shown in the output along with the date and time that it was created.

To create this folder and hide this output, we can assign it to $null as follows:

$null = New-Item -Path c:\users\bobbi\data1\south_data -ItemType Directory

The following screenshot shows how to use this syntax in practice:

PowerShell New-Item silent

Notice that no output was shown when we used the New-Item cmdlet this time and when we used the Dir command we can see that the folder named south_data was indeed created.

Note that we also could have casted the New-Item cmdlet to [void] to also create this new folder while hiding the output:

[void](New-Item -Path c:\users\bobbi\data1\south_data -ItemType Directory)

Feel free to use whichever method you prefer.

Note: You can find the complete documentation for the New-Item cmdlet in PowerShell here.

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

PowerShell: How to Copy and Rename a File
PowerShell: How to List Files in Directory by Date
PowerShell: How to Rename File Extension of Multiple Files

Leave a Reply