PowerShell works with objects. These objects have different properties and methods. Properties could give you details like when a file was created or the status of a service. I know a lot of people have blogged on this but I wanted to give you some examples of when I’ve had to use methods and what my thought process was.
Get-Member
Methods are actions that you can perform directly on the object. Methods and Properties are known as the objects members. Therefore we can use Get-member to see an objects properties and methods.
Get-ChildItem -Path c:\temp | Get-Member
The command above command is good example because Get-childitem returns two different types of objects, directory and File. You’ll see the type of object in the first line after you execute command.
TypeName: System.IO.DirectoryInfo
After that you’ll see the methods, properties and any aliases. I have only shown one of the objects here, but below that there is the System.IO.FileInfo object members.
Using Methods
I recently used methods for working with SharePoint on-prem and Web App Pools. I think it’s a lot easier to work with SharePoint online modules but I haven’t got there yet and for AppPools I recently discovered Desire State Configuration which makes things easier. In this post I’ll go over working with SharePoint lists.
Working with SharePoint lists
For the SharePoint project I had to add items to a SharePoint list. I won’t go in depth but just give you a few tips. First of all we use Get-SPWeb which creates an object we can then interact with to get more info from. This command returns a Microsoft.SharePoint.SPWeb object. Note: GM is an alias for Get-Member.
$spWeb = Get-SPWeb -Identity "https://intranet.ordobott.gi"
PS C:\> $spWeb | gm
TypeName: Microsoft.SharePoint.SPWeb
Choose a list
From the list of members (see above) we can see the Lists property and in the definition you can see that this returns a Microsoft.SharePoint.SPListCollection object. If you look for that on the web you can see that it has some xml properties, I think in the end it actually returns xml.
https://docs.microsoft.com/en-us/dotnet/api/microsoft.sharepoint.splistitemcollection?view=sharepoint-server
Therefore you can access specific items in the xml like you would when working with xml files. For example:
$splist = $spweb.lists["Users"]
As you can see I normally store the results in a variable to make it easier to work with. When you get data from the lists property you get another type of object with it’s own set of members. This time it’s a SPList object.
$splist | gm
TypeName: Microsoft.SharePoint.SPList
Name MemberType Definition
---- ---------- ----------
AddItem Method Microsoft.SharePoi
AddWorkflowAssociation Method Microsoft.SharePoi
AppendDataTable Method void AppendDataTab
BreakRoleInheritance Method void BreakRoleInhe
CheckPermissions Method void CheckPermissi
Delete Method void Delete()
Get items from the list
The SpList members include the GetItemByIdAllFields method and from the definition you can see it expects an integer.
GetItemByIdAllFields Method Microsoft.SharePoint.SPListItem GetItemByIdAllFields(int id)
I used this in a loop to get a full list and populate a custom PowerShell object.
for ($i = 1; $i -le $count; $i++) {
$item = $splist.GetItemByIdAllFields($i)
$properties = @{ComputerName = $item["ows_PC_x0020_Name"]
UserName = $item["Title"]
}
$obj = New-Object -Property $properties -TypeName psobject
Write-Output $obj
}
Before I knew what fields I needed to look for. I had to use the QueryFieldNames property of the SPListItemCollection Class. From this list you can see what the field names were because I wouldn’t have known to use ows_PC_x0020_Name for the PC name as the SharePoint list in the browser doesn’t show this, at least I don’t know how to check field names in that way.
$spweb.lists["local admin users"].Items.QueryFieldNames
Add items to list
To cut a long story short the SPList has an Items property, I was able to use the Add method on the $splist.items object but I’m not sure where the Add method comes from as I could not see it in the members of the object that SpList.items returns which is of the type SPListItem.
PS C:\> $splist.items | gm
TypeName: Microsoft.SharePoint.SPListItem
However if you look in the definition of the lists property it mentions the SPListItemCollection class which does have a Add method. Which you can see here: https://docs.microsoft.com/en-us/dotnet/api/microsoft.sharepoint.splistitemcollection?view=sharepoint-server#definition
Name MemberType Definition
---- ---------- ----------
Items Property Microsoft.SharePoint.SPListItemCollection Items {get;}
The add method creates a new object, where you can append the properties you need and finally you will need to use the update method to apply the changes to the added item.
PS C:\> $Newitem = $splist.items.add()
PS C:\> $Newitem | gm
TypeName: Microsoft.SharePoint.SPListItem
$Newitem = $splist.items.add()
$newitem["ows_PC_x0020_Name"] = $computername
$newitem["Title"] = $username
$newitem.update()
Note: when using methods that don’t expect a parameter you still need to include the parenthesis.
Conclusion
To be honest I am not sure how classes work, but I had to experiment a bit to get this to work which was my main goal. But I will try to find out more on classes. I got the basics to start working with SharePoint from Shane Young’s YouTube video.