Tag: PowerShell Create Folder

  • Create Folder If Not Exists in PowerShell

    Create Folder If Not Exists in PowerShell

    The PowerShell script included in this blog post checks if a specific folder exists and then creates it if it does not already exist.

    It does this by using the Test-Path command to check for the existence of the folder and the New-Item command to create it if it doesn’t exist. The script can be modified to check for and create any desired folder by changing the specified path.

    The following is included in this PowerShell demo:
    Using Test-Path to Check if a Folder Exists
    Creating a Folder with New-Item
    Modifying the Script for Others

    Using Test-Path to Check if a Folder Exists

    The first step in this script is to use the Test-Path command to check if the desired folder already exists. The Test-Path command returns a boolean value indicating whether the specified path exists.

    To check if a folder exists using PowerShell, we can use the following syntax:

    # Check if folder exists
    $path = "c:\temp\"
    If(!(test-path $path))

    Here, we are storing the path of the folder we want to check in the $path variable. The ! operator negates the boolean value returned by Test-Path, so the If statement will only execute if the folder does not exist.

    Creating a Folder with New-Item

    To create a new folder, we can use the New-Item command with the -ItemType parameter set to Directory. The -Force parameter ensures that the command will overwrite any existing files with the same name and the -Path parameter specifies the location where the new folder will be created.

    # create a new folder 
    New-Item -ItemType Directory -Force -Path $path

    Modifying the Script for Others

    To use this script to check for and create a different folder, simply modify the $path variable to the desired location. For example, to check for and create the c:\myfolder\ folder, you would use the following script:

    # create folder if not exists
    $path = "c:\myfolder\"
    If(!(test-path $path))
    {
        New-Item -ItemType Directory -Force -Path $path
    }
    

    By modifying the $path variable, you can use this script to check for and create any folder you need.

    PowerShell Create Folder if Not Exist

  • 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