Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Sitecore Powershell Image upload
function Update-MediaItem {
[Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)]
[Parameter(Position=1, Mandatory=$true)]
[Sitecore.Data.Items.MediaItem]$item = gi $mediaPath
[Sitecore.Resources.Media.Media] $media = [Sitecore.Resources.Media.MediaManager]::GetMedia($item);
$extension = [System.IO.Path]::GetExtension($filePath);
$stream = New-Object -TypeName System.IO.FileStream -ArgumentList $filePath, "Open", "Read"
$media.SetStream($stream, $extension);
function Find-MediaItem {
[Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$true)]
$query = "/sitecore/media library//*[@@name='{0}']" -f $mediaName
Write-Host $query
[Sitecore.Data.Items.MediaItem]$item = Get-Item master: -Query $query | Select -First 1
return $item
#write-host "$SitecoreDataFolder"
Get-ChildItem "$SitecoreDataFolder\upload\*.jpg" -Recurse| ForEach-Object {
$file = $_;
Write-Host $file.FullName
$mediaItem = Find-MediaItem $file.BaseName
$mediaPath = "{0}{1}" -f $([Sitecore.Constants]::MediaLibraryPath), $mediaItem.MediaPath
$mediaId = "{0}" -f $mediaItem.ID
Write-Host $mediaId $mediaPath
if ($mediaId -ne "") {
Update-MediaItem $file.FullName $mediaPath
Remove-Item $file
Write-Host "Imported and deleted"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment