Create Empty File in PowerShell

Create Empty File in PowerShell

Using New-Item Cmdlet

Use the New-Item cmdlet to create an empty .txt file in PowerShell.

We used the New-Item cmdlet with -Path, -Name, and -ItemType parameters to create an empty .txt file using Windows PowerShell. Let’s break down the above command to understand it more clearly.

The New-Item cmdlet creates a new item and sets its value by adding initial content or data to the file. We can use this cmdlet to create a different types of items depending on what location we are using for the items. For instance, in registry, the New-Item creates registry entries and keys, and if we are in FileSystem, then the New-Item creates folders and files.

The -Name parameter is used to specify the new item’s name, while -Path is used to write the path of the location where our new file will reside. We used "File1.txt" as the name of the new item and "E:\NewFolder" as the location’s path.

Remember, if we do not use the -Path parameter, the New-Item cmdlet will save the newly created file in the current directory. We can write the new item’s name in the -Name or -Path parameter. See the following examples.

Note that the names of the new items we pass using the -Name parameter will be created relative to the -Path parameter’s value. For the New-Item cmdlet, the -Path behaves like other cmdlets’ LiteralPath parameter.

The worth noting point is that wildcard characters will not be interpreted. So, for instance, we can not create a .txt file with a name which contains an asterisk (*).

The -ItemType parameter is used to specify the new item’s type. Now, what values can we use with this parameter? It depends on the current provider. For instance, if we are in a location which is in FileSystem then, we can use File, Directory, SymbolicLink, Junction, HardLink as values for the -ItemType parameter.

Use the ni Alias

Use ni to create an empty .txt file in PowerShell.

The ni is not a different cmdlet, but it is a built-in alias for the New-Item and works as New-Item does. You can go back to the previous section to learn about the New-Item cmdlet, its parameters, and its working.

As we saw, we got some informational text in the above output, including mode, length, file name, and the last update date/time. What if we want to avoid getting this type of information and are only concerned with the file’s creation? In that case, the following solution will work for us.

Using Out-File Cmdlet

Use the Out-File cmdlet to create an empty file (.txt) in PowerShell.

Here, we used the Out-File cmdlet with the -FilePath parameter to create a .txt file without content. We can also use this command as Out-File -FilePath "E:\NewFolder\File3.txt" to specify the path; otherwise, the File3.txt would be created in the current directory.

The primary use of Out-File is to send the output (received from the previous process) to a file. Look at the following example, where we get details of all the currently running processes using the Get-Process cmdlet and forward this information to Out-File using the pipe sign (|).

Now, the Out-File will write the received data to the specified text file we can print on the PowerShell using the Get-Content cmdlet. Note that the Out-File will create the File3.txt file if it does not exist already.

Now, think of Out-File, where we write nothing to the file. What does it mean to write nothing to a file? It means we are passing null using $null as follows. Here, $null is an automatic variable representing null in PowerShell.

For the above code, the Get-Content cmdlet will show nothing because the File3.txt file is empty. Are you thinking that if we are writing nothing in the File3.txt file, then why not create a file without writing anything?

Yes, you got it right! Therefore, we can omit the $null variable and use the Out-File as follows to create an empty .txt file.

Using the fsutil file Command

Use the fsutil file command to create an empty .txt file in PowerShell.

We used the fsutil file command to create an empty File4.txt file in the current directory. We used this command because we were in a situation where we had to specify the length of the file while creating it. Remember, the fsutil file command will not work if the value for the length parameter is missing (in our case, the length is 1000).

Note that the fsutil file command can do various things; for instance, it can find a file by username, set a short name of a file, set a valid data length of a file, or create a new file, etc. based on what parameter is passed.

For example, in our case, the fsutil file created an empty text file due to using the createnew parameter. On the other hand, it can find the files belonging to a particular user if used with the findbyid parameter.

The last numerical value (1000) in the above code snippet is used for the length parameter, which is used to specify the valid data length of a file.

Using Text Editor

Use the text editor in PowerShell to create an empty .txt File.

As soon as we hit the Enter key after writing the above command, an empty File5.txt will be created (if it does not exist) and opened as well. We used notepad as a text editor; you can use any of your choices, for instance, nano, vim, etc.

Using echo Command

Use the echo command with greater than sign (>) in the PowerShell to create an empty .txt File.

The primary use of echo is to write, but we are writing an empty string to a file. It is the most basic command, fine for learning purposes but not recommended for professional use where you are required to create a brand new but empty file.

So, we have learned various approaches for creating an empty .txt file in PowerShell, but all the approaches were focused on creating a single file. So, let’s learn the use of the for loop to create n empty text files.

Using for Loop

To create an empty text file in PowerShell:

  • Use a for loop, which iterates from 1(including) to 11 (excluding).
  • Create a variable containing the path and file name.
  • Use the New-Item cmdlet to create an empty text file.

For this example, we used to loop over the given range, which is 1 to 11, where 11 is not included. Next, we created a $path variable, which holds the location’s path and file’s name formatted using the -f operator.

Here, the formatting operator (-f) was used to concatenate the string with the value of the $i variable. Finally, we used the New-Item cmdlet, discussed in the first section, to create a new text file.

That’s all about how to create empty file in PowerShell.

Was this post helpful?

Related Posts

  • Remove property from Object in PowerShell
    27 January

    Remove Property from Object in PowerShell

    Table of ContentsUsing PSObject.Properties.Remove() MethodUsing -ExcludeProperty Parameter Using PSObject.Properties.Remove() Method To delete a property from an object in PowerShell, Use the New-Object cmdlet to create an object. Use the PSObject.Properties.Remove() method to remove a specific property from an object by providing the property’s name as a string parameter. [crayon-63d3fe4e294b6484244202/] [crayon-63d3fe4e294b9899944523/] In the above code, we […]

  • Create File if not exists PowerShell
    26 January

    Create File If Not Exists in PowerShell

    Table of ContentsUsing the if-else StatementUse the New-Item CmdletUse the Out-File CmdletUsing the try-catch StatementUse the New-Item CmdletUse the Out-File CmdletUsing One-Liners Using the if-else Statement We can use the if-else statement to create a file as follows: Use the New-Item Cmdlet To create a file if it does not exist in PowerShell: Use the […]

  • Check if folder exists in PowerShell
    26 January

    Check if Folder Exists in PowerShell

    Table of ContentsUsing Built-in Cmdlets and MethodsUse the Test-Path CmdletUse the [System.IO.Directory]::Exists() MethodUse the Get-Item CmdletOther CmdletsCreating Custom Alias Using Built-in Cmdlets and Methods We can use built-in cmdlets and methods to check if a directory exists. Use the Test-Path Cmdlet To check if a folder exists, use the Test-Path cmdlet. [crayon-63d3fe4e2e175430217727/] [crayon-63d3fe4e2e179123182015/] PowerShell’s Test-Path […]

  • Convert Month name to number in PowerShell
    23 January

    Convert Month Number to Name in PowerShell

    Table of ContentsUsing the Get-Culture() CmdletConvert the Hardcoded Month Number to the NameConvert the Month Number from a Date to the NameConvert the Current Month Number to the NameUsing the [cultureinfo]::InvariantCulture Property Using the Get-Culture() Cmdlet We can use the Get-Culture cmdlet in the following three possible ways: To Convert the Hardcoded Month Number to […]

  • Delete empty folders in PowerShell
    23 January

    Delete Empty Folders in PowerShell

    Table of ContentsUsing the Remove-Item with Get-ChildItem CmdletUsing the Get-ChildItem and ForEach-ObjectUsing the User-defined Function There are multiple ways that we can use to delete the empty folders in PowerShell but before going into the details, let’s have a look at the following directory structure to know how many and which folders are empty. [crayon-63d3fe4e2e410646674313/] […]

  • PowerShell- Where-Object with multiple conditions
    23 January

    PowerShell – Where-Object with Multiple Conditions

    Table of ContentsUsing Where-Object Cmdlet with Multiple ConditionsScript BlockComparison Statements Using Where-Object Cmdlet with Multiple Conditions In PowerShell, we have various comparison and logical operators that we can use with the Where-Object cmdlet to apply multiple conditions. Let’s explore a few scenarios below. [crayon-63d3fe4e28766245347892/] [crayon-63d3fe4e2876c231491351/] The above command returned an item where the extension of […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.