PowerShell script to get the ShowInEditForm property for a site column. i do not want to use PnP PowerShell - powershell

I am trying this script to get the ShowInEditForm property of a site column and then set it to False:-
Connect-SPOService -Url https://****-admin.sharepoint.com/
Get-SPOSite -Identity https://***.sharepoint.com/
$column = Get-SPOField -Identity "CarNumber"
but i am getting this error:-
Get-SPOField : The term 'Get-SPOField' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
any advice please?

The Get-SPOField is not a valid cmdlet, you should use the Get-PnPField cmdlet to get the fields.

Try using this PowerShell:
#Set Variables
$SiteURL= "https://crescent.sharepoint.com/sites/Marketing"
$InternalName = "MyEditor"
#Setup Credentials to connect
$Cred = Get-Credential
Try {
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
#Get the Web
$Web = $Ctx.Web
$Ctx.Load($Web)
#Get the Site Column
$SiteColumn = $Web.Fields.GetByInternalNameOrTitle($InternalName)
$Ctx.Load($SiteColumn)
$Ctx.ExecuteQuery()
#Get the Site Column's Name,Type, ID and Group
$SiteColumn | Select Title, TypeDisplayName, ID, Group
}
Catch {
write-host -f Red "Error: " $_.Exception.Message
}
Source: SharePoint Online: PowerShell to Get Site Columns

Related

Sharepoint update metadata with Powershell problem with syntax?

I am unable to update the date and capital letters on my sharepoint via powershella script. All the others work, only this one doesn't work.
Can you help me? I have been tired for several hours.
My sharepoint metadata:
My script for update metadata in Sharepoint.
Thank you so much for help
I tested DATA_AUDYTU as Date field, NAZWA_SIECI as Single Line Text field in my side with the sample code snippet below, it's working as expected:
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.Runtime.dll"
#Set parameter values
$SiteURL="https://Tenant.sharepoint.com"
$FileRelativeUrl="/doc2/test.txt"
Try {
#Get Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials
$File = $Ctx.web.GetFileByServerRelativeUrl($FileRelativeUrl)
$Ctx.Load($File)
$Ctx.Load($File.ListItemAllFields)
$Ctx.ExecuteQuery()
#Set Metadata of the File
$ListItem = $File.ListItemAllFields
$Listitem["NAZWA_SIECI"] = "TEST"
$Listitem["DATA_AUDYTU"]="12/24/2020"
$ListItem.Update()
$Ctx.ExecuteQuery()
Write-host -f Green "File's Metadata has been Updated Successfully!"
}
Catch {
write-host -f Red "Error Updating Metadata of the File!" $_.Exception.Message
}
Column details:

Sharepoint PnP List: How to "Group by" columns using Powershell

I want to group a Sharepoint list by a column using powershell, so when users access this list, it's already grouped.
I know the List settings already has this Here, but I want to be able to control this via a powershell script that resets the list to data read from a local server.
Below images provide additional clarity of my question.
Columns not grouped together
Columns grouped together
Please take a reference of below powershell script:
#Set Variables
$SiteURL = "https://abc.sharepoint.com/sites/s01"
$ListName = "your list name"
$Username='admin#abc.onmicrosoft.com'
$Password = 'xxxx'
#region Credentials
[SecureString]$SecurePass = ConvertTo-SecureString $Password -AsPlainText -Force
[System.Management.Automation.PSCredential]$PSCredentials = New-Object System.Management.Automation.PSCredential($Username, $SecurePass)
#endregion Credentials
#Connect to PNP Online
Connect-PnPOnline -Url $SiteURL -Credentials $PSCredentials
#Get the Client Context
$Context = Get-PnPContext
#Get the List View
$View = Get-PnPView -Identity "All Items" -List $ListName
$Context.Load($View)
$Context.ExecuteQuery()
$View.ViewQuery
$ViewQuery = '<GroupBy Collapse="TRUE" GroupLimit="30"><FieldRef Name="num" /></GroupBy>' + $View.ViewQuery
#Update the view Query
$View.ViewQuery = $ViewQuery
$View.Update()
$Context.ExecuteQuery()
More references:
https://www.sharepointdiary.com/2018/04/sharepoint-online-powershell-to-update-list-view.html
https://learn.microsoft.com/en-us/powershell/module/sharepoint-pnp/get-pnpview?view=sharepoint-ps
BR

Error when executing PowerShell script for SharePoint Online

So, i'm trying to run this SPO PowerShell script that microsoft provides in this link:
https://learn.microsoft.com/en-us/powershell/module/sharepoint-online/export-spouserinfo?view=sharepoint-ps on the "example 2". However, when i try to run the script on PowerShell ISE, i get the following error: "Parameter missing for 'Output Folder' argument. Specify a 'System.String' type parameter and try again." I tried to change the arguments, input my site collection, creating a .csv file on the folder, but nothing changes this error message, what am i doing wrong?
Here is the code i'm using:
$sites = Get-SPOSite -IncludePersonalSite $true
$user = "xxxxxx#domain.com"
foreach ($site in $sites)
{
Export-SPOUserInfo -LoginName $user -site $site.Url -OutputFolder
"D:"
}
Thanks in advance!
Writing to the root of a drive is really not a best practice. Always use a folder of the root, unless there is a very valid reason to put a file there. Yet, that is not your use case as presented.
$sites = Get-SPOSite -IncludePersonalSite $true
$user = "xxxxxx#domain.com"
foreach ($site in $sites)
{
Export-SPOUserInfo -LoginName $user -site $($site.Url) -OutputFolder 'D:\SPOSiteData'
}
Your string must be all on one line if not properly terminated for multi-line. For example, using PowerShell Splatting
about_Splatting - PowerShell | Microsoft Docs
$ExportSPOUserInfoSplat = #{
LoginName = $user
site = $($site.Url)
OutputFolder = 'D:\SPOSiteData'
}
Export-SPOUserInfo #ExportSPOUserInfoSplat
Te line wrapping, which it seems you copied and pasted, is that way because of page space not a code requirement.

PowerShell to Export SharePoint Sites and Subsides with security groups

I'm attempting to come up with a powershell script that would allow me to export a list of sites and subsites and the permission groups there in to a CSV.
I'm familiar with using the cmdlts but not building whole scripts.
I'm able to use:
Get-SPOSiteGroup | Export-CSV C:\...
To export site groups to a CSV but it doesn't include the name of the sites they are in.
I also found a script online that would print out the sites and subsite in my site collection here:
https://sharepoint.stackexchange.com/questions/101176/powershell-to-list-all-sites-and-subsites-in-sharepoint-online
I'm not sure how to marry the information. I'm trying to export to a CSV a list of sites and subsites and the security groups there in.
I try to run:
get-sposite | Get-SPOSiteGroup **webdite**
And get this error message:
"Get-SPOSiteGroup : The input object cannot be bound to any parameters
for the command either because the command does not take pipeline
input or the input and its properties do not match any of the
parameters that take pipeline input"
I'm not sure how to get all of this to work together.
Get-SPOSiteGroup cmdlet accepts Site parameter, so site groups per every site collection within a tenant could be retrieved like this:
Connect-SPOService -Url $adminUrl
$sitesInfo = Get-SPOSite
#Retrieve and print all sites
foreach ($site in $sitesInfo) {
Write-Host 'Site collection:' $site.Url
Get-SPOSiteGroup -Site $site.Url
Write-Host '-----------------------------'
}
To retrieve in addition groups per every web site, the following script could be utilized:
$adminUrl = "https://<tenant>-admin.sharepoint.com"
$UserName = "<username>#<tenant>.onmicrosoft.com"
$Password = "--password goes here--"
$SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
$creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
$pscreds = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $userName, $SecurePassword
Connect-SPOService -Url $adminUrl -Credential $pscreds
$sitesInfo = Get-SPOSite
foreach ($site in $sitesInfo) {
Write-Host 'Site collection:' $site.Url
#1. Retrieve and print site info
#Get-SPOSiteGroup -Site $site.Url
$AllWebs = Get-SPOWebs -Url $site.Url -Credential $creds
#2.Retrieve and print webs info (groups Title property in this case)
foreach ($web in $AllWebs) {
$web.Context.Load($web.RoleAssignments.Groups)
$web.Context.ExecuteQuery()
$web.RoleAssignments.Groups.GetEnumerator() | % { Write-Host $_.Title }
}
Write-Host '-----------------------------'
}
Key points:
to retrieve webs within a site collection Get-SPOWebs.ps1 is utilized
here
to get groups per web site Web.RoleAssignments.Groups is used

Powershell not getting SharePoint Online List

I'm trying to write a script for automatic VM Backups, that you can also start manually in SharePoint Online. I'm planning to keep a list with the names of the VMs in SharePoint and wanted to start writing the script by getting the list of VMs. I think my code looks good so far, but when I want to get the list by title it returns nothing.
Here's the code:
[Reflection.Assembly]::LoadFile(([System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client").location))
[Reflection.Assembly]::LoadFile(([System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.runtime").location))
Function Get-SPOContext([string]$Url,[string]$UserName,[string]$Password)
{
$SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
$context = New-Object Microsoft.SharePoint.Client.ClientContext($Url)
$context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
return $context
}
Function Get-ListItems([Microsoft.SharePoint.Client.ClientContext]$Context, [String]$ListTitle) {
$list = $Context.Web.Lists.GetByTitle($listTitle)
$qry = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()
$items = $list.GetItems($qry)
$Context.Load($items)
$Context.ExecuteQuery()
return $items
}
$UserName = read-host "Please enter your name:"
$PasswordReadHost = Read-Host -Prompt "Enter password" -AsSecureString
$Password = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($PasswordReadHost))
$Url = "https://domain.sharepoint.com/"
$ListTitle = "VMList"
$context = Get-SPOContext -Url $Url -UserName $UserName -Password $Password
$items = Get-ListItems -Context $context -ListTitle $ListTitle
When I run this, add a breakpoint at the end and check the value of the $list variable I get two error messages:
Exception calling "ExecuteQuery" with "0" argument(s): "The list
'VMList' does not exist on the website with the URL
'https://domain.sharepoint.com'.
and
An error occurred while enumerating through a collection: The
collection has not been initialized. It has not been requested or the
request has not been executed. It may need to be explicitly
requested.
After a lot of googling I tried changing many things, but nothing seemed to work. I guess my question is: Why can't the list be found on SharePoint and what does it mean to initialize a collection or if it's just an authorization issue?