How to Upload a .css file on the folder within the Style Library of my site collection using PowerShell. I am having a style file called style_bg.css. How to get the folder within the style library through powershell.I have written the below line of code.

        if((Get-PSSnapin | Where {$_.Name -eq   
        "Microsoft.SharePoint.PowerShell"}) -eq $null) {
        Add-PSSnapin Microsoft.SharePoint.PowerShell;
       #Site Collection where you want to upload files
       $siteCollUrl  = "http://srvr1:123/sites/Geology/"

         $libraryName = "Style Library" #  can i enter folder name here ?

       #Physical/Network location of files
       $reportFilesLocation  = "C:\Maddy\Scripts\Styles"
      $spSourceWeb = Get-SPWeb $siteCollUrl;
     $spSourceList = $spSourceWeb.Lists[$libraryName];
     if($spSourceList -eq $null)
       Write-Host "The Library $libraryName could not be found."
        $files = ([System.IO.DirectoryInfo] (Get-Item  
     foreach($file in $files)
      #Open file
         $fileStream = ([System.IO.FileInfo] (Get-Item 
        #Add file
       $folder =  $spSourceWeb.getfolder($libraryName)
        Write-Host "Copying file $file to $libraryName..."

But i am not able to insert into the particular sub folder within the style library using the above. I am stuck with where I am missing. And if its checked out after insert , how to check in and publish.

1 Answer 1


For uploading file to a particular folder you need to initialize it as

$folderName = "FolderToUpload";
$folder =  $spSourceList.getfolder($spSourceWeb.Url + "/" + $libraryName + "/" + $folderName)

For file check in you can use

$spFile.CheckIn("Comments", 1)

To Approve File you can use

  • thanks. Am getting the error "The file "srvr1:123/sites/Geology/Style Library/myfolder/style_bg.css" is not checked out. You must first check out this document before making changes."
    – samolpp2
    Commented May 22, 2015 at 10:26
  • You can checkout using $spFile.CheckOut() Commented May 22, 2015 at 10:29
  • $folder = $web.getfolder($docLibrary.Title+"/"+$cssfoldername) Write-Host $_.Name " ..is the file name...." $spFile = $folder.Files.Add($folder.Url + "/" + $_.Name, $contents, $true) Write-Host "checking out file" $spFile.CheckOut() want to know, whether should i check out the already existing file before adding files to folder or after caling this function folder.files.add() ? i am trying to overriding the already existing file with few changes in my css file. There are some 40 + site collections so,I can't add this css file manually.
    – samolpp2
    Commented May 22, 2015 at 10:39
  • You should checkout before Adding file. Commented May 22, 2015 at 10:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.