My dirctory is D:\\Main
. I want to create a file named file.js
in the directory only i.e. D:\\Main
. I use the code
type nul > file.js
. It creates the file but also displayes an error message saying that nul
folder wasn't found. Am I doing something wrong? How do I improve the code to stop the error message?

- 763
- 6
- 19
-
2New-Item -Path "D:\Main\file.js" – f6a4 Dec 16 '19 at 15:46
2 Answers
What you attempted to use is valid for CMD.EXE, but in PowerShell, type
is an alias for Get-Content
, which expects a valid device or file. However, the null device is not available in PowerShell, although a variable $null
will return the desired value. You should instead use
New-Item -Path . -Name "file.js" -Value $null
or
Set-Content -Path .\file.js -Value $null
to create an empty file.

- 9,773
- 2
- 21
- 33
I didn't get a satisfactory answer so I searched the command list for PowerShell on Microsoft's website.
Use
Set-Content -path D:\\Main\file.js
Then it will ask for values *(e.g. [-Value0], [-Value1], ...)." Values are what what you want to type in the file e.g. [-Value0] will contain what will be in the first line of the code of file.js. Pressing enter will move to next line i.e. [-Value1]. Pressing enter without typing anything in a value will exit taking values and save the file.
The above command can be shortened like:
sc
for Set-Content
& .
for D:\\Main
because it is your default directory. You can also skip the -path
term. So the new shortened command will be:
sc .\file.js

- 763
- 6
- 19