Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9259367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:38:30+00:00 2026-06-18T12:38:30+00:00

I have a powershell script that I am trying to add webparts to a

  • 0

I have a powershell script that I am trying to add webparts to a sharepoint page automatically. The script below is wrapped in a FOREach-Object and it works fine on the first iteration however on the second itereation it is returning some unwanted information to the powershell screen (and logs I am creating).

I am looking for a way to prevent that from returning anything other than the write-host statements. I originally thought it might be an exception however It looks like the web parts are still being added with no issue and I can’t find any reference to an exeption in the return info.

Here is what I have thus far:

$addCsv | ForEach-Object{

            $tempwebURL = $_.'SiteURL'
            $pageUrl = $_.'Page'
            $varWebPartZone = $_.'WebPartZone'
            $varChrome = $_.'Chrome'
            $varTitle = $_.'Title'          
            $varPosition = [int]$_.'Position'

            #get current site
            $tempweb= Get-SPWeb -Identity $tempwebURL
            Write-Host ("Adding Web Part to :"+ $tempweb.Title)

            #add webpart
            [System.Xml.XmlTextReader]$oxmlReader = new-object System.Xml.XmlTextReader("Path removed");
            $webpartmanager=$tempweb.GetLimitedWebPartManager( $pageUrl,  [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)    
            [System.Web.UI.WebControls.WebParts.WebPart] $oWebPart
            $err = ''
            $oWebPart = new-object $webpartmanager.ImportWebPart($oxmlReader,[ref]$_.Exception.Message)
            $oWebPart.Title = $varTitle
            $oWebPart.ChromeType=$varChrome
            $webpartmanager.AddWebPart($oWebPart, $varWebPartZone, $varPosition)| Out-Null 
            $oWebPart.Dispose() 
            $oxmlReader.Close()
            $webpartmanager.Dispose()
            $tempweb.Dispose()
            $num++

}

Below is the information being printed to the screen which I do not want:

AllowClose                         : True
AllowConnect                       : True
AllowEdit                          : True
AllowHide                          : True
AllowMinimize                      : True
AllowZoneChange                    : True
AuthorizationFilter                :
CatalogIconImageUrl                :
ChromeState                        : Normal
ChromeType                         : None
ConnectErrorMessage                :
Description                        :
Direction                          : NotSet
DisplayTitle                       : Last Date Modified Explorer
ExportMode                         : None
HasUserData                        : False
HasSharedData                      : False
Height                             :
HelpMode                           : Navigate
HelpUrl                            :
Hidden                             : False
IsClosed                           : False
ImportErrorMessage                 : Cannot import this Web Part.
IsShared                           : True
IsStandalone                       : False
IsStatic                           : False
Subtitle                           :
Title                              : Last Date Modified Explorer
TitleIconImageUrl                  :
TitleUrl                           :
Verbs                              : {}
WebBrowsableObject                 : Removed
Width                              :
Zone                               :
ZoneIndex                          : 0
Controls                           :
BackImageUrl                       :
DefaultButton                      :
GroupingText                       :
HorizontalAlign                    : NotSet
ScrollBars                         : None
Wrap                               : True
AccessKey                          :
Attributes                         : System.Web.UI.AttributeCollection
BackColor                          : Color [Empty]
BorderColor                        : Color [Empty]
BorderWidth                        :
BorderStyle                        : NotSet
ControlStyle                       : System.Web.UI.WebControls.PanelStyle
ControlStyleCreated                : True
CssClass                           :
Style                              : System.Web.UI.CssStyleCollection
Enabled                            : True
EnableTheming                      : True
Font                               :
ForeColor                          : Color [Empty]
HasAttributes                      : False
SkinID                             :
TabIndex                           : 0
ToolTip                            :
ClientID                           : g_adea57ed_5d6d_40ee_b468_ce85e37f5e9a
ID                                 : g_adea57ed_5d6d_40ee_b468_ce85e37f5e9a
EnableViewState                    : True
NamingContainer                    :
BindingContainer                   :
Page                               :
TemplateControl                    :
Parent                             :
TemplateSourceDirectory            :
AppRelativeTemplateSourceDirectory :
Site                               :
Visible                            : True
UniqueID                           : g_adea57ed_5d6d_40ee_b468_ce85e37f5e9a

I also welcome any “you could do this better by…” comments. Thanks again.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T12:38:31+00:00Added an answer on June 18, 2026 at 12:38 pm

    My guess would be that the following line is the culprit:

    [System.Web.UI.WebControls.WebParts.WebPart] $oWebPart
    

    In the first iteration this won’t do much, it’s just a cast of $null. But in later iterations it retains the value of the previous iteration and outputs it. You don’t need that line anyway as it does nothing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have developed a Powershell script that needs to be run automatically daily. This
I have a powershell script that modifies transport rules. It works perfectly from powershell
I have a PowerShell script/function that works great when I use it in my
I have a powershell script that works wonderfully from the powershell shell and from
I have the following powershell script that I am trying to adapt for use
I have a powershell script that will generate an email for users whose password
I have a complex Powershell script that gets run as part of a SQL
I have a function in a powershell script that is supposed to untar my
I trying to write a PowerShell script (learning at the same time). I have
I am currently trying to write a little powershell script (I have no experience

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.