PowerShell Commands and Scripts for Sitecore Item: Part-2

In my previous blog, we had discussed Installation of the PSE module with Sitecore and Out of The Box Tools. In this blog, I am discussing with you some basic development of PowerShell Command and Scripts.

  1. Determine PowerShell version: Execute the below command will give you the all the details of installed PowerShell like Version, Edition, SPE module version, etc. In my case, I have installed SPE module 6.2.  
    $PSVersionTable
    PowerShell-Commands-and-Scripts-1
  2. Directory and Text file: Create a Directory into the system. Write text into the text file and save that text file in the system directory. PowerShell-Commands-and-Scripts-2
  3. Get Sitecore Item: You can get Sitecore Item by Path, ID, Sitecore Query. I am giving you an example with Path.
    PowerShell-Commands-and-Scripts-3 In the above, I have set parameter value -ErrorAction SilentlyContinue which means the Get-Item command will execute without any error shown on the console in case it does not found any item on the path.   Now you can perform various operations on the SC Item like update the field values, remove Item, move the item, copy Item, set new template, or rendering on that Item. Here I am updating the field value:PowerShell-Commands-and-Scripts-4
  4. Set another template on Sitecore Item:
     Set-ItemTemplate -Path "master:/sitecore/content/Helixbase/Home" -Template
          "/sitecore/templates/Project/Page Types/Generic Page"    
  5. Set Rendering Parameter on Sitecore Item: Suppose you have defined rendering a parameter named HeaderClass and by default, its values are Layout-No-Image and you want to update its values with Layout-With-Images then use the below scripts:
    PowerShell-Commands-and-Scripts-5
  6. Get Child Items:
        $allChildItem = Get-ChildItem -Path "master:/sitecore/content/Helixbase/Home"
    If you want to get the child items from all the subdirectories and nested files, then use the below command:
        $allChildItem = Get-ChildItem -Path $itemPath -Recurse
    If you want to fetch child items on the condition of a particular template ID, then use the below command:
        $allChildItem = Get-ChildItem -Path $itemPath -Recurse | where-object 
        {$_.TemplateID -eq "{1D5C266A-112F-4EA2-A69E-E4865ACE2200}"}
  7. Assign Multilist Field value of Item: Suppose you have Multilist filed in your item named Models. PowerShell-Commands-and-Scripts-6
  8. Read CSV file:
        $importData = Import-CSV "C:\path\sample_csv_file.csv"  
    If the CSV file is on your local system, then the above command will work fine. If you want to read it from the deployed environment or your project is deployed on the Azure CM server and you want to read CSV file from the root directory, then use the below command:
    	   $folder = [Sitecore.IO.FileUtil]::MapPath("~/path")
           $importData = Import-CSV "$($folder)/sample_csv_file.csv"
    In case your CSV file is encoded into any other format like UTF8 and you have HTML content into CSV columns then the Sitecore Item field shows special characters in the content. To overcome this issue use the below command to import CSV:
       $importData = Import-CSV "C:\path\sample_csv_file.csv" -Encoding "UTF8"

In my next blog, I will let you know in detail about "Import CSV Data in Sitecore Using PowerShell".

Reference: https://doc.sitecorepowershell.com/

Happy Sitecoreing ðŸ˜Š

Comments

Popular posts from this blog

Sitecore Installation Error: Failed to Start Service 'Sitecore Marketing Automation Engine'

Import CSV Data in Sitecore Using PowerShell: Part-3

Sitecore : The database principal owns a schema in the database, and cannot be dropped message