New-Item (Microsoft.PowerShell.Management) - PowerShell (2024)

  • Reference
Module:
Microsoft.PowerShell.Management

Creates a new item.

Syntax

New-Item [-Path] <String[]> [-ItemType <String>] [-Value <Object>] [-Force] [-Credential <PSCredential>] [-WhatIf] [-Confirm] [<CommonParameters>]
New-Item [[-Path] <String[]>] -Name <String> [-ItemType <String>] [-Value <Object>] [-Force] [-Credential <PSCredential>] [-WhatIf] [-Confirm] [<CommonParameters>]
New-Item [-Path] <string[]> -ConnectionURI <uri> [-ItemType <string>] [-Value <Object>] [-Force] [-Credential <pscredential>] [-WhatIf] [-Confirm] [-OptionSet <hashtable>] [-Authentication <AuthenticationMechanism>] [-CertificateThumbprint <string>] [-SessionOption <SessionOption>] [-Port <int>] [<CommonParameters>]
New-Item [[-Path] <string[]>] -Name <string> [-ItemType <string>] [-Value <Object>] [-Force] [-Credential <pscredential>] [-WhatIf] [-Confirm] [-OptionSet <hashtable>] [-Authentication <AuthenticationMechanism>] [-CertificateThumbprint <string>] [-SessionOption <SessionOption>] [-ApplicationName <string>] [-Port <int>] [-UseSSL] [<CommonParameters>]
New-Item [-Path] <string[]> [-ItemType <string>] [-Value <Object>] [-Force] [-Credential <pscredential>] [-WhatIf] [-Confirm] [-Options <ScopedItemOptions>] [<CommonParameters>]
New-Item [[-Path] <string[]>] -Name <string> [-ItemType <string>] [-Value <Object>] [-Force] [-Credential <pscredential>] [-WhatIf] [-Confirm] [-Options <ScopedItemOptions>] [<CommonParameters>]

Description

The New-Item cmdlet creates a new item and sets its value. The types of items that can be createddepend on the location of the item. For example, in the file system, New-Item creates files andfolders. In the registry, New-Item creates registry keys and entries.

New-Item can also set the value of the items that it creates. For example, when it creates a newfile, New-Item can add initial content to the file.

Examples

Example 1: Create a file in the current directory

This command creates a text file that is named "testfile1.txt" in the current directory. The dot('.') in the value of the Path parameter indicates the current directory. The quoted text thatfollows the Value parameter is added to the file as content.

New-Item -Path . -Name "testfile1.txt" -ItemType "file" -Value "This is a text string."

Example 2: Create a directory

This command creates a directory named "Logfiles" in the C: drive. The ItemType parameterspecifies that the new item is a directory, not a file or other file system object.

New-Item -Path "c:\" -Name "logfiles" -ItemType "directory"

Example 3: Create a profile

This command creates a PowerShell profile in the path that is specified by the $profile variable.

You can use profiles to customize PowerShell. $profile is an automatic (built-in) variable thatstores the path and file name of the "CurrentUser/CurrentHost" profile. By default, the profile doesnot exist, even though PowerShell stores a path and file name for it.

In this command, the $profile variable represents the path of the file. ItemType parameterspecifies that the command creates a file. The Force parameter lets you create a file in theprofile path, even when the directories in the path do not exist.

After you create a profile, you can enter aliases, functions, and scripts in the profile tocustomize your shell.

For more information, see about_Automatic_Variablesand about_Profiles.

New-Item -Path $profile -ItemType "file" -Force

Example 4: Create a directory in a different directory

This example creates a new Scripts directory in the "C:\PS-Test" directory.

The name of the new directory item, "Scripts", is included in the value of Path parameter,instead of being specified in the value of Name. As indicated by the syntax, either command formis valid.

New-Item -ItemType "directory" -Path "c:\ps-test\scripts"

Example 5: Create multiple files

This example creates files in two different directories. Because Path takes multiple strings,you can use it to create multiple items.

New-Item -ItemType "file" -Path "c:\ps-test\test.txt", "c:\ps-test\Logs\test.log"

Example 6: Use wildcards to create files in multiple directories

The New-Item cmdlet supports wildcards in the Path parameter. The following command creates atemp.txt file in all of the directories specified by the wildcards in the Path parameter.

Get-ChildItem -Path C:\Temp\Directory: C:\TempMode LastWriteTime Length Name---- ------------- ------ ----d----- 5/15/2019 6:45 AM 1 Oned----- 5/15/2019 6:45 AM 1 Twod----- 5/15/2019 6:45 AM 1 ThreeNew-Item -Path C:\Temp\* -Name temp.txt -ItemType File | Select-Object FullNameFullName--------C:\Temp\One\temp.txtC:\Temp\Three\temp.txtC:\Temp\Two\temp.txt

The Get-ChildItem cmdlet shows three directories under the C:\Temp directory. Using wildcardsthe New-Item cmdlet creates a temp.txt file in all of the directories under the currentdirectory. The New-Item cmdlet outputs the items you created, which is piped to Select-Objectto verify the paths of the newly created files.

Example 7: Create a symbolic link to a file or folder

This example creates a symbolic link to the Notice.txt file in the current folder.

$link = New-Item -ItemType SymbolicLink -Path .\link -Target .\Notice.txt$link | Select-Object LinkType, TargetLinkType Target-------- ------SymbolicLink {.\Notice.txt}

In this example, Target is an alias for the Value parameter. The target of the symbolic linkcan be a relative path. Prior to PowerShell v6.2, the target must be a fully-qualified path.

Beginning in PowerShell 7.1, you can now create to a SymbolicLink to a folder on Windows using arelative path.

Example 8: Use the -Force parameter to attempt to recreate folders

This example creates a folder with a file inside. Then, attempts to create the same folder using-Force. It will not overwrite the folder but simply return the existing folder object with thefile created intact.

PS> New-Item -Path .\TestFolder -ItemType DirectoryPS> New-Item -Path .\TestFolder\TestFile.txt -ItemType FilePS> New-Item -Path .\TestFolder -ItemType Directory -Force Directory: C:\Mode LastWriteTime Length Name---- ------------- ------ ----d----- 5/1/2020 8:03 AM TestFolderPS> Get-ChildItem .\TestFolder\ Directory: C:\TestFolderMode LastWriteTime Length Name---- ------------- ------ -----a---- 5/1/2020 8:03 AM 0 TestFile.txt

Example 9: Use the -Force parameter to overwrite existing files

This example creates a file with a value and then recreates the file using -Force. This overwritesthe existing file, as you can see by the length property.

PS> New-Item ./TestFile.txt -ItemType File -Value 'This is just a test file' Directory: C:\Source\TestMode LastWriteTime Length Name---- ------------- ------ -----a---- 5/1/2020 8:32 AM 24 TestFile.txtNew-Item ./TestFile.txt -ItemType File -Force Directory: C:\Source\TestMode LastWriteTime Length Name---- ------------- ------ -----a---- 5/1/2020 8:32 AM 0 TestFile.txt

Note

When using New-Item with the -Force switch to create registry keys, the command will behavethe same as when overwriting a file. If the registry key already exists, the key and allproperties and values will be overwritten with an empty registry key.

Parameters

-ApplicationName

This is a dynamic parameter made available by the WSMan provider. The WSMan provider andthis parameter are only available on Windows.

Specifies the application name in the connection. The default value of the ApplicationNameparameter is WSMAN.

For more information, see New-WSManInstance.

Type:String
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-Authentication

This is a dynamic parameter made available by the WSMan provider. The WSMan provider andthis parameter are only available on Windows.

Specifies the authentication mechanism to be used at the server.

For more information, see New-WSManInstance.

Type:AuthenticationMechanism
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-CertificateThumbprint

This is a dynamic parameter made available by the WSMan provider. The WSMan provider andthis parameter are only available on Windows.

Specifies the digital public key certificate (X509) of a user account that has permission to performthis WSMan action. Enter the certificate thumbprint of the certificate.

For more information, see New-WSManInstance.

Type:String
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-Confirm

Prompts you for confirmation before running the cmdlet.

Type:SwitchParameter
Aliases:cf
Position:Named
Default value:False
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-ConnectionURI

This is a dynamic parameter made available by the WSMan provider. The WSMan provider andthis parameter are only available on Windows.

Specifies the connection endpoint for WSMan.

For more information, see New-WSManInstance.

Type:Uri
Position:Named
Default value:None
Required:True
Accept pipeline input:False
Accept wildcard characters:False

-Credential

Note

This parameter isn't supported by any providers installed with PowerShell. To impersonate anotheruser or elevate your credentials when running this cmdlet, use Invoke-Command.

Type:PSCredential
Position:Named
Default value:Current user
Required:False
Accept pipeline input:True
Accept wildcard characters:False

-Force

Forces this cmdlet to create an item that writes over an existing read-only item. Implementationvaries from provider to provider. Even using the Force parameter, the cmdlet can't overridesecurity restrictions.

Beginning in PowerShell 7.4, this parameter also allows you to overwrite an existing Junction.Previously, this would fail with a "cannot be removed because it is not empty" error.

Type:SwitchParameter
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-ItemType

Specifies the provider-specified type of the new item. The available values of this parameter dependon the current provider you are using.

If your location is in a FileSystem drive, the following values are allowed:

  • File
  • Directory
  • SymbolicLink
  • Junction
  • HardLink

Note

Creating a SymbolicLink type on Windows requires elevation as administrator. However, Windows 10(build 14972 or newer) with Developer Mode enabled no longer requires elevation creating symboliclinks.

In a Certificate drive, these are the values you can specify:

  • Certificate Provider
  • Certificate
  • Store
  • StoreLocation

For more information see about_Providers.

Type:String
Aliases:Type
Position:Named
Default value:None
Required:False
Accept pipeline input:True
Accept wildcard characters:False

-Name

Specifies the name of the new item. You can specify the name of the new item in the Name orPath parameter value, and you can specify the path of the new item in Name or Pathvalue. Items names passed using the Name parameter are created relative to the value of thePath parameter.

Type:String
Position:Named
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:False

-Options

This is a dynamic parameter made available by the Alias provider. For more information, seeNew-Alias.

Specifies the value of the Options property of an alias.

Valid values are:

  • None: The alias has no constraints (default value)
  • ReadOnly: The alias can be deleted but can't be changed without using the Force parameter
  • Constant: The alias can't be deleted or changed
  • Private: The alias is available only in the current scope
  • AllScope: The alias is copied to any new scopes that are created
  • Unspecified: The option isn't specified
Type:ScopedItemOptions
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-OptionSet

This is a dynamic parameter made available by the WSMan provider. The WSMan provider andthis parameter are only available on Windows.

Passes a set of switches to a service to modify or refine the nature of the request.

For more information, see New-WSManInstance.

Type:Hashtable
Aliases:OS
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-Path

Specifies the path of the location of the new item. The default is the current location whenPath is omitted. You can specify the name of the new item in Name, or include it inPath. Items names passed using the Name parameter are created relative to the value of thePath parameter.

For this cmdlet, the Path parameter works like the LiteralPath parameter of other cmdlets.Wildcard characters are not interpreted. All characters are passed to the location's provider. Theprovider may not support all characters. For example, you can't create a filename that contains anasterisk (*) character.

Type:String[]
Position:0
Default value:Current location
Required:False
Accept pipeline input:True
Accept wildcard characters:False

-Port

This is a dynamic parameter made available by the WSMan provider. The WSMan provider andthis parameter are only available on Windows.

Specifies the port to use when the client connects to the WinRM service.

For more information, see New-WSManInstance.

Type:Int32
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-SessionOption

This is a dynamic parameter made available by the WSMan provider. The WSMan provider andthis parameter are only available on Windows.

Defines a set of extended options for the WS-Management session.

For more information, see New-WSManInstance.

Type:SessionOption
Aliases:SO
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-UseSSL

This is a dynamic parameter made available by the WSMan provider. The WSMan provider andthis parameter are only available on Windows.

Specifies that the Secure Sockets Layer (SSL) protocol should be used to establish a connection tothe remote computer. By default, SSL isn't used.

For more information, see New-WSManInstance.

Type:SwitchParameter
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-Value

Specifies the value of the new item. You can also pipe a value to New-Item.

Type:Object
Aliases:Target
Position:Named
Default value:None
Required:False
Accept pipeline input:True
Accept wildcard characters:False

-WhatIf

Shows what would happen if the cmdlet runs. The cmdlet isn't run.

Type:SwitchParameter
Aliases:wi
Position:Named
Default value:False
Required:False
Accept pipeline input:False
Accept wildcard characters:False

Inputs

Object

You can pipe a value for the new item to this cmdlet.

Outputs

DictionaryEntry

The cmdlet returns a DictionaryEntry object when creating a new environment variable.

DirectoryInfo

The cmdlet returns a DirectoryInfo object when creating a new directory in the filesystem.

FileInfo

The cmdlet returns a FileInfo object when creating a new file in the filesystem.

AliasInfo

The cmdlet returns an AliasInfo object when creating a new alias.

FunctionInfo

The cmdlet returns a FunctionInfo object when creating a new function.

PSVariable

The cmdlet returns a PSVariable object when creating a new variable.

Notes

PowerShell includes the following aliases for New-Item:

  • All platforms:
    • ni

New-Item is designed to work with the data exposed by any provider. To list the providersavailable in your session, type Get-PsProvider. For more information, seeabout_Providers.

  • Clear-Item
  • Copy-Item
  • Get-Item
  • Invoke-Item
  • Move-Item
  • Remove-Item
  • Rename-Item
  • Set-Item
  • about_Providers
New-Item (Microsoft.PowerShell.Management) - PowerShell (2024)

FAQs

How do you create a new item in PowerShell? ›

Creating new items

To create a new item in the filesystem, use the New-Item cmdlet. Include the Path parameter with path to the item, and the ItemType parameter with a value of file or directory . When typing a registry path, be sure to include the colon ( : ) in the PowerShell drive names, HKLM: and HKCU: .

What is the new item property in PowerShell? ›

The New-ItemProperty cmdlet creates a new property for a specified item and sets its value. Typically, this cmdlet is used to create new registry values, because registry values are properties of a registry key item. This cmdlet does not add properties to an object.

What does new object do in PowerShell? ›

The New-Object cmdlet creates an instance of a . NET Framework or COM object. You can specify either the type of a . NET Framework class or a ProgID of a COM object.

How to fix permission denied in PowerShell? ›

1 answer
  1. Ensure you are logged into the computer with local administrator rights.
  2. Right-click or hold “Shift” and select “Windows PowerShell“ > “Run as administrator“.
  3. Run PowerShell as admin.
  4. Now try to run the same set-executionpolicy command to change the execution policy. It should complete successfully.
Dec 6, 2022

How do I add an object in PowerShell? ›

To create a new object type, use the Add-Type cmdlet. You can also use the Export-Clixml cmdlet to save the instance of the object, including the additional members, in a file. Then you can use the Import-Clixml cmdlet to re-create the instance of the object from the information that's stored in the exported file.

What does "new item" mean? ›

Related Definitions

New Item means an item not previously stocked, or a price posted with Commission by the Wholesaler on or since his last price posting.

What is the difference between mkdir and new-item in PowerShell? ›

In PowerShell, MD is an alias to MKDIR. Also, MKDIR is an alias to the New-Item cmdlet. So that means if you type MD, MKDIR, or New-Item in PowerShell, all those commands have the same effect: running the New-Item cmdlet.

How to create a custom object in PowerShell? ›

Creating a Custom Object

PowerShell allows the creation of custom objects. To create a custom object called dog , we use the New-Object cmdlet with the PSCustomObject type name. By default, new custom objects have no properties and four methods.

How do you create a file object in PowerShell? ›

To create a single file with PowerShell, you can use the New-Item cmdlet. For example, to create a new text file named “example. txt”, you can use the command: New-Item -ItemType File -Path "C:\path\to\example.

Why is my PowerShell restricted? ›

The PowerShell execution policy is the setting that determines which type of PowerShell scripts (if any) can be run on the system. By default it is set to “Restricted“, which basically means none.

How do I grant permissions in PowerShell? ›

To set a new permission using Powershell you'll need to create the ACE with the group and required permissions, then get the existing ACL from the folder, add the ACE to it and then save it back.

Why is PowerShell access denied? ›

To solve the Access to the path is denied PowerShell problem, you should make sure that: The Windows PowerShell is running as Administrator. The provided path is correct in Add-Content . The file is not located directly in the c:\ drive, it should be inside a folder in the root drive.

How do I create an empty Object in PowerShell? ›

To create an empty array of objects in PowerShell by initializing an array with the array subexpression operator @() . For example, $emptyArray = @() gives you an empty array to which you can add objects using $emptyArray += New-Object -TypeName PSObject .

How to create a new file in PowerShell? ›

Powershell - Create Text File
  1. In this example, we're creating a new text file named test.txt. Type the following command in PowerShell ISE Console New-Item D:\temp\test\test. txt. ...
  2. In this example, we're adding content to test. txt. ...
  3. In this example, we're reading content of test. txt.

How do I create a new string in PowerShell? ›

A String can be defined in PowerShell by using the single or double-quotes. Both the strings are created of the same System. String object type.

How to create an Object in Active Directory using PowerShell? ›

Windows PowerShell
  1. Identify the domain in which you want to create computers in AD.
  2. Identify the LDAP attributes you need configure.
  3. Compile the script.
  4. Execute it in Windows PowerShell.

References

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Neely Ledner

Last Updated:

Views: 6069

Rating: 4.1 / 5 (42 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Neely Ledner

Birthday: 1998-06-09

Address: 443 Barrows Terrace, New Jodyberg, CO 57462-5329

Phone: +2433516856029

Job: Central Legal Facilitator

Hobby: Backpacking, Jogging, Magic, Driving, Macrame, Embroidery, Foraging

Introduction: My name is Neely Ledner, I am a bright, determined, beautiful, adventurous, adventurous, spotless, calm person who loves writing and wants to share my knowledge and understanding with you.