If you want to connect PI System from powershell, then "PowerShell Tools for the PI System" is the first option.

"PowerShell Tools for the PI System" allows us to connect PI System from Powershell.

Basically it is for administration tasks and it is easy to use.

Here is the help file from Live Library.



On the other hand, Powershell allows to load AFSDK library and we can use it.

If you are familiar with AFSDK, it is easier to write code with Powershell.

Previously I wrote Python and AFSDK example blog.

Python 3.6 and AFSDK example (PIthon!)

Now this blog post is Powershell and AFSDK.


For loading AFSDK from Powershell, only one line allows us to do it.

$AFSDKAssembly = [Reflection.Assembly]::LoadWithPartialName("OSIsoft.AFSDK")


After that, we can use AFSDK from Powershell.

Here is my code example.


* This code includes write value to PI Tag.

Please change write tag name within the code.

$writept = [OSIsoft.AF.PI.PIPoint]::FindPIPoint($piServer,"PleaseEnterWriteTagName")

Also this code use NuGreen database. Please change element, attribute name as your AF object name.


I uploaded the code to my github.

GitHub - kenji0711/Powershell-and-AFSDK-example: It shows how to use Powershell + AFSDK


# Load AFSDK  
$AFSDKAssembly = [Reflection.Assembly]::LoadWithPartialName("OSIsoft.AFSDK")

# PI Data Archive
"[Let's play with PI Data Archive]"
$piServers = New-Object OSIsoft.AF.PI.PIServers
$piServer = $piServers.DefaultPIServer
Write-host("PI Data Archive Name: {0}" -f $piServer.Name)

# Get PIPoint
Write-host("TagName: {0}" -f $pt.Name)

# Current Value
$snap = $pt.CurrentValue()
Write-host("Current Value: {0}, Timestamp: {1}" -f $snap.Value, $snap.Timestamp)

# Recorded Values
$timerange = New-Object OSIsoft.AF.Time.AFTimeRange("*-3h", "*")
$recorded = $pt.RecordedValues($timerange,[OSIsoft.AF.Data.AFBoundaryType]::Inside,"",$false,1000)
"Recoded Values :"
Write-host ($recorded| select Timestamp, value)

# Interpolated Values  
$span = New-Object OSIsoft.AF.Time.AFTimeSpan(New-TimeSpan -hours 1) 
$interpolated = $pt.InterpolatedValues($timerange, $span, "", $false)  
"Interpolated Values :"
Write-host ($interpolated| select Timestamp, value)

# Summaries Values  
$summaries = $pt.Summaries($timerange, $span, 
"Summaries Values :"

"Write value to the PI Tag :"
$writept = [OSIsoft.AF.PI.PIPoint]::FindPIPoint($piServer,"PleaseEnterWriteTagName")
$aftime = New-Object OSIsoft.AF.Time.AFTime("t+9h")
$writeval = New-Object OSIsoft.AF.Asset.AFValue(10,$aftime)
Write-host("Tag : {0} Wrote Value: {1} Timestamp: {2}" -f $writept.Name, $writeval.Value, $writeval.Timestamp.LocalTime.ToString())

"[Let's play with PI AF]"
# AFServer
$afServers = New-Object OSIsoft.AF.PISystems  
$afServer = $afServers.DefaultPISystem
Write-host("AFServer Name: {0}" -f $afServers.Name)

# AFDatabase
$DB = $afServer.Databases["NuGreen"]
Write-host("DataBase Name: {0}" -f $DB.Name)# Line Break

# Get Element
$Element = $DB.Elements["NuGreen"].Elements["Little Rock"].Elements["Extruding Process"].Elements["Equipment"].Elements["K-435"]
Write-host("Element Name: {0}" -f $Element.Name)

# Get attribute
$Attribute = $Element.Attributes["Steam Flow"]
Write-host("Attribute Name: {0}" -f $Attribute.Name)
$Attval = $Attribute.GetValue()
Write-host("Value: {0}, Timestamp: {1}" -f $Attval.Value, $Attval.Timestamp)

# Create new element with attribute  
"Try to create new Element with Attribute :"
if (!$DB.Elements["Powershell New Element"])
    $newelement = $DB.Elements.Add("Powershell New Element")
    $newelement.Description = "Created element from Powershell"
    $newattribute = $newelement.Attributes.Add("Powershell Attribute")  
    $newattribute.DataReferencePlugIn = $afServer.DataReferencePlugIns["PI Point"]
    $newattribute.DataReference.ConfigString = "cdt158"  
    Write-host("Created new Element : {0}" -f $newelement.Name)
    "The element is already created"


Here is the results of the screenshot.

Using Powershell and AFSDK requires PSA license for production environment.

It is really useful because it does not require Visual Studio. (Python need instalation of the python itself but windows standard OS contains Powershell by default)

Since OSIsoft help file does not contain any Powershell or Python code example,

it is useful to share the example code.

I will use it if the custom application requires simple logic. It is quite handy to use!

AFSDK is the king of the Data Access to the PI System.

Enjoy the AFSDK and Powershell.