Powershell with FileSystem batch file

Scenario 1: Calling batch file from powershell.
Note:MKDIR/RMDIR - Create and remove directories Learn some basic batch commands.

Example

 
    ECHO - Displays text on the screen
    @ECHO OFF - Hides the text that is normally output
    START - Run a file with it's default application
    REM - Inserts a comment line in the program
    MKDIR/RMDIR - Create and remove directories
    DEL - Deletes a file or files
    COPY - Copy a file or files
    XCOPY - Allows you to copy files with extra options
    FOR/IN/DO - This command lets you specify files
    

Copy and Try it


create sample file .bat or .cmd file (.bat is the old 16-bit naming convention, and .cmd is for 32-bit Windows)

Example

 
        MKDIR c:\test\example1
        MKDIR c:\test\example2
    

Copy and Try it


Now we want to give call to this batch file from powershell.

Example

 
        Write-Host "Call the batch from pwoershell"
        C:\test\batch.bat
        Write-Host "Done folders (example1, example2) should created.."
    

Copy and Try it

Scenario 2: Calling powershell script file from batch. 1. Create sample powershell file (powershell-to-create-directories.ps1)create 2 example1 and example2 folders like we did in earlier example. Here we will do in opposite manner..

Example

 
        MKDIR c:\test\example1
        MKDIR c:\test\example2
    

Copy and Try it

Example

 
        @echo off
        Powershell.exe -File  C:\test\Call-batch-file-from-powershell.ps1
        pause
    

Copy and Try it

Note: you might need permissions to execute powershell script on your machine.

Scenario 3: Passing arguments to batch to powershell. This is powershell script we are passing 2 agrument from batch file and inside powershell script we will create 2 folder with these agrument name like Myarg1 Myarg2. Batch File

Example

 
    @echo off
    Powershell.exe -File  C:\test\Pass-Arguments-from-batchfile-to-powershell.ps1 Myarg1 Myarg2
    pause
    

Copy and Try it

Powershell File

Example

 
        $MyFirstArg=$args[0]
        $MySecondArg=$args[1]
        MKDIR c:\test\$MyFirstArg
        MKDIR c:\test\$MySecondArg
    

Copy and Try it





Output After double clicking or executing batch file 2 folders should get created with given name like here Myarg1 Myarg2

Scenario 4: Passing arguments to powershell from batch. Comming soon and also we will be adding more common scenarios.