Tag: PowerShell Create File

  • Create New Files and Folders in PowerShell

    Create New Files and Folders in PowerShell

    The New-Item cmdlet will create new files or folders in PowerShell. Amend the -ItemType parameter to File or Directory and include a Name for the file or folder.

    This blog post will walk through the process of creating a new file and a new folder using PowerShell. I add an extra tip at the end on adding text to a file with PowerShell too.

    For more examples and up-to-date information on creating files with New-Item, we can refer to Microsoft Documentation. They have PowerShell New-Item command examples for creating PowerShell profiles and symbolic links.

    As described, the following is contained within this demo:
    # Create New Folder in PowerShell
    # Create New File in PowerShell
    # Add Text to File PowerShell

    Create Folder PowerShell

    To create a new folder in PowerShell, run New-Item -ItemType Directory -Name dirName, amending the name as desired.

    This will create a folder in the directory you are currently navigated to within the PowerShell Terminal. So for the example below, the new folder is being created in c:\users\pete\

    # PowerShell create new folder
    New-Item -ItemType Directory -Name Test_Stuff
    Create New Folder PowerShell

    Create File PowerShell

    To make a new file in PowerShell, run New-Item -ItemType File -Name testFile amending the name as desired. This creates a file within your current working terminal directory.

    This is the same command we ran above to create a folder, but this time changing the ItemType parameter.

    # PowerShell new file
    New-Item -ItemType File -Name Test_File.txt
    Create File PowerShell

    Add Text to File PowerShell

    Now for a bonus PowerShell tip that you did not search or ask for. How to add text to a file in PowerShell.

    To add text to a file in PowerShell we can use the Add-Content cmdlet, and we can verify with Get-Content to check the text in a file.

    # PowerShell add text to file
    Add-Content .\Test_File.txt -Value 'Hi hi!... just a wee message here.'
    
    # PowerShell show file content
    Get-Content .\Test_File.txt
    PowerShell Add Data to File