none
Creating Batch file of a powershell command

    Question

  • Here is my power shell command -

    Export-SPWeb -Identity "http://some site:14616" -Path "D:\exportItems\MyPlanDocuments.cmp" -ItemUrl "/Plan Documents" -CompressionSize 200

    now i want to create .bat file that can be run directly from command prompt without powershell usage.

    Tuesday, May 08, 2012 3:30 PM

Answers

  • Hi!

    You should create PowerShell script file (name it something like sp_script.ps1) with following commands:

    Add-PSSnapin "Microsoft.SharePoint.Powershell"
    Export-SPWeb -Identity "http://some site:14616" -Path "D:\exportItems\MyPlanDocuments.cmp" -ItemUrl "/Plan Documents" -CompressionSize 200

    Then you can run this script from command line like following:

     %windir%\system32\windowspowershell\v1.0\powershell.exe sp_script.ps1

    Tuesday, May 08, 2012 5:53 PM
  • the just open up notepad and enter the line:

    %windir%\system32\windowspowershell\v1.0\powershell.exe -command sp_script.ps1

    Save it as run_sp_script.bat.

    And if you want to call upon it from anywhere, then easiest way is save the .bat file in C:\Windows


    Dirk Van den Berghe

    Wednesday, May 09, 2012 7:20 AM
  • You cannot execute PowerShell commands outside of a PowerShell shell. You need to load a PowerShell shell first from the DOS command shell. It is the same for SQL. You cannot run a SQL query from the command prompt without running a SQL command shell.

    Dirk Van den Berghe

    Wednesday, May 09, 2012 9:08 AM
  • then you need to use a stsadm command and not Powershell as you stated in your original question. STSADM is a DOS command line tool for administering SharePoint. Almost all of the stsadm commands available can be achieved using PowerShell.

    the counterpart of your Powershell command using stsadm would then be:

    stsadm -o export  -url http://some site:14616/Plan Documents -filename "D:\exportItems\MyPlanDocuments.cmp" -cabsize 200

    complete syntax for stsadm export function:

    stsadm.exe -o export
               -url <URL to be exported>
               -filename <export file name>
                   [-overwrite]
               [-includeusersecurity]
               [-haltonwarning]
               [-haltonfatalerror]
               [-nologfile]
               [-versions <1-4>
                   1 - Last major version for files and list items (default)
                   2 - The current version, either the last major or the last minor
                   3 - Last major and last minor version for files and list items
                   4 - All versions for files and list items]
               [-cabsize <integer from 1-1024 megabytes> (default: 24)]
               [-nofilecompression]
               [-quiet]
               [-usesqlsnapshot]


    Dirk Van den Berghe

    Wednesday, May 09, 2012 9:42 AM
  • thanks ,dirk ,:)
    Wednesday, May 09, 2012 11:12 AM

All replies

  • Hi!

    You should create PowerShell script file (name it something like sp_script.ps1) with following commands:

    Add-PSSnapin "Microsoft.SharePoint.Powershell"
    Export-SPWeb -Identity "http://some site:14616" -Path "D:\exportItems\MyPlanDocuments.cmp" -ItemUrl "/Plan Documents" -CompressionSize 200

    Then you can run this script from command line like following:

     %windir%\system32\windowspowershell\v1.0\powershell.exe sp_script.ps1

    Tuesday, May 08, 2012 5:53 PM
  • Yes ,thanks its working fine  ,

    but i want this in a  batch file that can be run directly through cmd prompt(and that file can be at any location no need to put that file on perticular location) . so please help me out 




    Wednesday, May 09, 2012 5:14 AM
  • the just open up notepad and enter the line:

    %windir%\system32\windowspowershell\v1.0\powershell.exe -command sp_script.ps1

    Save it as run_sp_script.bat.

    And if you want to call upon it from anywhere, then easiest way is save the .bat file in C:\Windows


    Dirk Van den Berghe

    Wednesday, May 09, 2012 7:20 AM
  • ok great solution ,but i don't want to refer any .ps1 file in .bat file , i want to put cmd directly  into the bat file , 
    Wednesday, May 09, 2012 9:02 AM
  • You cannot execute PowerShell commands outside of a PowerShell shell. You need to load a PowerShell shell first from the DOS command shell. It is the same for SQL. You cannot run a SQL query from the command prompt without running a SQL command shell.

    Dirk Van den Berghe

    Wednesday, May 09, 2012 9:08 AM
  • 1. But i came across , one batch file in which we are using stsadm command to export sharepoint  site 

    2. And to run that batch file , we have to put our .ps1 file in the folder 


    C:\Windows\system32\windowspowershell\v1.0\    is there any way , to put the batch file and .ps1 file on the same location , not on c drive any drive .

    how ever if it is possible to put the .ps1 and .bat at same location ,my problem resolved , 
    Wednesday, May 09, 2012 9:32 AM
  • then you need to use a stsadm command and not Powershell as you stated in your original question. STSADM is a DOS command line tool for administering SharePoint. Almost all of the stsadm commands available can be achieved using PowerShell.

    the counterpart of your Powershell command using stsadm would then be:

    stsadm -o export  -url http://some site:14616/Plan Documents -filename "D:\exportItems\MyPlanDocuments.cmp" -cabsize 200

    complete syntax for stsadm export function:

    stsadm.exe -o export
               -url <URL to be exported>
               -filename <export file name>
                   [-overwrite]
               [-includeusersecurity]
               [-haltonwarning]
               [-haltonfatalerror]
               [-nologfile]
               [-versions <1-4>
                   1 - Last major version for files and list items (default)
                   2 - The current version, either the last major or the last minor
                   3 - Last major and last minor version for files and list items
                   4 - All versions for files and list items]
               [-cabsize <integer from 1-1024 megabytes> (default: 24)]
               [-nofilecompression]
               [-quiet]
               [-usesqlsnapshot]


    Dirk Van den Berghe

    Wednesday, May 09, 2012 9:42 AM
  • thanks ,dirk ,:)
    Wednesday, May 09, 2012 11:12 AM