PowerShell入门 - 2 (Using the New-Item Cmdlet)

Creating a New File or Folder

New-Item is a quick and easy way to create a new file or folder on your computer. For example, suppose you want to create a new directory named Windows PowerShell within the C:\Scripts folder. To do that call New-Item along with: 1) the full path to the new folder; and, 2) the new item type (which you can specify using the -type parameter and the value directory). The command in question will look like this:

New-Item c:\scripts\Windows PowerShell -type directory

To create a new file follow the same procedure, specifying the full path name but this time setting the type to file. This command creates the file C:\Scripts\New_file.txt:

New-Item c:\scripts\new_file.txt -type file

If the item you are trying to create already exists you’ll get back an error message similar to this:

New-Item : The file 'C:\scripts\new_file.txt' already exists.

However, you can override the default behavior by including the -force parameter:

New-Item c:\scripts\new_file.txt -type file -force

If you use -force the existing file New_file.txt will be replaced by the new, empty file.

And speaking of new, empty files, you can also use the -value parameter to add some data to your new file. This command adds the phrase This is text added to the file at the same time it creates New_file.txt:

New-Item c:\scripts\new_file.txt -type file -force -value "This is text added to the file"

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值