Today I am going to share a PowerShell script that you can use to upload images in bulk to Sitecore using Sitecore PowerShell.
Steps to follow
Step 1: Firstly create a folder SitecoreImages in your Sitecore local instance. For me the path of Sitecore local instance is C:\inetpub\wwwroot\Sitecore100Learningsc.dev.local.
Note: In place of C:\inetpub\wwwroot\Sitecoredevsc.dev.local use your Sitecore instance path.
Step 2: Now, create a CSV file that contains the name that is to be used for the image and a link for the image. Follow the structure shown in the image below for the CSV file.
Step 3: Now, follow the below PowerShell script and make changes according to the comments mentioned in the script.
function New-MediaItem {
[CmdletBinding()]
param(
[Parameter(Position = 0, Mandatory = $true, ValueFromPipeline = $true)]
[ValidateNotNullOrEmpty()]
[string]$filePath,
[Parameter(Position = 1, Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$mediaPath
)
$mco = New-Object Sitecore.Resources.Media.MediaCreatorOptions
$mco.Database = [Sitecore.Configuration.Factory]::GetDatabase("master");
$mco.Language = [Sitecore.Globalization.Language]::Parse("en");
$mco.Versioned = [Sitecore.Configuration.Settings+Media]::UploadAsVersionableByDefault;
$mco.Destination = "$($mediaPath)/$([System.IO.Path]::GetFileNameWithoutExtension($filePath))";
$mc = New-Object Sitecore.Resources.Media.MediaCreator
$mc.CreateFromFile($filepath, $mco);
}
# Reading CSV file
## Location of your CSV file
$CsvList = Import-CSV "D:\MediaImages.csv"
foreach ($row in $CsvList) {
$MediaUrl = $row.MediaItemUrl
$MediaName = $row.MediaItemName + ".jpg"
## Created folder in Sitecore local instacnce (SitecoreImages)
$Loc = "$AppPath\SitecoreImages\$MediaName"
$wc = New-Object System.Net.WebClient
$response = $wc.DownloadFile($MediaUrl, $Loc);
$images = Get-ChildItem "$AppPath\Myimages\*.jpg" -Recurse
foreach ($image in $images) {
$imageName = $image.Name
$MediaImagePath = "$AppPath\Myimages\$imageName"
Write-Host $MediaImagePath
# Check if the image already exists in the Media Library.
if (Test-Path $MediaImagePath.TrimEnd(".$FileExtension")) {
Write-Host "Image $imageName already exists... skipping the Image"
}
# If doesn't exists, upload the image
else {
Write-Host "Uploading Image $imageName ..."
New-MediaItem $MediaImagePath "$([Sitecore.Constants]::MediaLibraryPath)/Feature/MyImage"
Write-Host "Uploading Image in Medialibrary $imageName ... Success."
Write-Host "Removing file $imageName"
Remove-Item $image
}
}
}
Step 4: Open the PowerShell ISE from Development Tools in Sitecore and copy the script to the PowerShell.
Step 5: Execute the script and you can see the images are uploaded in your Sitecore Media Library at the path specified in the Script.
References
Upload multiple images - https://stackoverflow.com/questions/45998458/how-do-i-upload-multiple-images-in-specific-folder-in-sitecore-programmatically
Bulk loading images - https://himadritechblog.wordpress.com/2015/05/02/bulk-loading-images-in-sitecore-media-library-using-sitecore-powershell-extension-spe/
Mass uploading Media - https://jammykam.wordpress.com/2016/03/11/mass-uploading-media/
Automate upload Media - https://sitecore.stackexchange.com/questions/28364/how-can-i-automate-the-upload-media-library-files-available-in-folder-under-app
Happy Coding, Coders for Life 😊