Search code examples
powershellnul

How to create a file in the directory in PowerShell


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?


Solution

  • 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.