This snippet adds fields from a CSV file to a SharePoint List. This also has lookup field support where it checks the look up source and validates the data. also has an ensure user people field example. Also an example of a CSV field whose columns cantain spaces.
#variables
$statusListName = "Status"
$siteListName = "Site"
$nomenclatureListName = "Nomenclature"
$propertyTypeListName = "PropertyType"
$owningListName = "Department"
$networkListName = "Network"
$inventoryListName = "Inventory"
# CSV path/File name
$contents = Import-Csv "CombinedHardware.csv"
# Web URL
$web = Get-SPWeb -Identity "https://homeportxxxx"
# SPList name
$statusList = $web.Lists[$statusListName]
$list = $web.Lists[$inventoryListName]
$siteList = $web.Lists[$siteListName]
$nomenclatureList= $web.Lists[$nomenclatureListName]
$propertyTypeList = $web.Lists[$propertyTypeListName]
$owningList = $web.Lists[$owningListName]
$networkList = $web.Lists[$networkListName]
# Iterate for each list column
$i = 0
foreach ($row in $contents) {
$i
$i++
$item = $list.Items.Add()
$item["Title"] = $row."Asset ID";
$item["Alternative ID"] = $row."Alterinative ID" -replace "[\W]", ""
$item["RFID"] = $row."RFID"
if($($row."Status").length -gt 0 )
{
$statusLookupItem = $statusList.Items | where {$_["Title"] -eq $row."Status"}
if($statusLookupItem -eq $null)
{
$csvValue = $row."Status"
write-host "$csvValue not found in Status lookup list"
"$csvValue not found in Status lookup list" | out-file "f:\temp\dataMigration.txt" -append
}
else
{
$item["Status"] = $statusLookupItem.ID
}
}
if($($row."Last Physical Inventory Date").length -gt 1 ) {$item["Last_x0020_Physical_x0020_Invent"] = $row."Last Physical Inventory Date"}
if($($row."Site").length -gt 0 )
{
$siteLookupItem = $siteList.Items | where {$_["Title"] -eq $row."Site"}
if($siteLookupItem -eq $null)
{
$csvValue = $row."Site"
write-host "$csvValue not found in site lookup list"
"$csvValue not found in site lookup list" | out-file "f:\temp\dataMigration.txt" -append
}
else
{
$item["Site"] = $siteLookupItem.ID
}
}
$item["Building"] = $row."Building"
$item["Floor"] = $row."Floor" -replace "[\W]", ""
$item["Room_x002f_Column_x002f_Area"] = $row."Room/Column/Area" -replace "[\W]", ""
if($($row."Nomenclature").length -gt 0 )
{
$nomenclatureLookupItem = $nomenclatureList.Items | where {$_["Title"] -eq $row."Nomenclature"}
if($nomenclatureLookupItem -eq $null)
{
$csvValue = $row."Nomenclature"
write-host "$csvValue not found in Nomenclature lookup list"
"$csvValue not found in Nomenclature lookup list" | out-file "f:\temp\dataMigration.txt" -append
}
else
{
$item["Nomenclature"] = $nomenclatureLookupItem.ID #lookup
}
}
#$item["Physical_x0020_Description"] =
$item["MANUFACTURER_x0020_NAME"] = $row."Manufacturer Name"
$item["Manufacturer_x0020_Part_x0020_Nu"] = $row."Manufacturer Part Number"
$item["Manufacturer_x0020_Model_x0020_N"] = $row."Manufacturer Model Number"
$item["Serial_x0020_Number"] = $row."Serial Number"
#$item["Year_x0020_of_x0020_Manufacture"] =
if($($row."Manufacturer End of Life").length -gt 1){$item["Manufacturer_x0020_End_x0020_of_"] = $row."Manufacturer End of Life"}
if([string]($($row."Useful Life") -as [int])){$item["Useful_x0020_Life"] = $row."Useful Life"} else {$item["Useful_x0020_Life"] = 0}
if($($row."Warranty Expiration Date").length -gt 1 ){$item["Warranty_x0020_Expiration"] = $row."Warranty Expiration Date"}
if($($row."Maintenance Expiration Date").length -gt 1 ){$item["Maintenance_x0020_Expiration_x00"] = $row."Maintenance Expiration Date"}
#$item["JML_x0020_Document_x002f_Contrac"] =
if([string]($($row."Acquisition Cost") -as [int])){$item["Acquisition_x0020_Cost"] = $row."Acquisition Cost"} else {$item["Acquisition_x0020_Cost"] = 0}
if($($row."Acquisition Date").length -gt 1 ){$item["Acquisition_x0020_Date"] = $row."Acquisition Date"}
if($($row."N4523A Receipt Date").length -gt 1 ){$item["V4523A_x0020_Receipt_x0020_Date"] = $row."N4523A Receipt Date"}
if($($row."Property Type").length -gt 0 )
{
$propertyTypeLookupItem = $propertyTypeList.Items | where {$_["Title"] -eq $row."Property Type"}
if($propertyTypeLookupItem -eq $null)
{
$csvValue = $row."Property Type"
write-host "$csvValue not found in Property Type lookup list"
"$csvValue not found in Property Type lookup list" | out-file "f:\temp\dataMigration.txt" -append
}
else
{
$item["Property_x0020_Type"] = $propertyTypeLookupItem.ID #lookup
}
}
if($($row."Owning Department/Branch").length -gt 1 )
{
$owningLookupItem = $owningList.Items | where {$_["Title"] -eq $row."Owning Department/Branch"}
if($owningLookupItem -eq $null)
{
$csvValue = $row."Owning Department/Branch"
write-host "$csvValue not found in Department lookup list"
"$csvValue not found in Department lookup list" | out-file "f:\temp\dataMigration.txt" -append
}
else
{
#$item["Owning_x0020_Department_x005c_Br"] = $owningLookupItem.ID #lookup
}
}
if($($row."POC (last- first MI)").length -gt 1 )
{
try {$user = $web.EnsureUser($row."POC (last- first MI)")} catch{}
$item["POC_x002f_CUSTODIAN_x0027_S_x002"] = $user
}
#$item["In_x0020_Service_x0020_Date"] =
#$item["IUID"] =
if($($row."Network").length -gt 1 )
{
$networkTypeLookupItem = $networkList.Items | where {$_["Title"] -eq $row."Network"}
if($networkTypeLookupItem -eq $null)
{
$csvValue = $row."Network"
write-host "$csvValue not found in Network lookup list"
"$csvValue not found in Network lookup list" | out-file "f:\temp\dataMigration.txt" -append
}
else
{
$item["Network"] = $networkTypeLookupItem.ID #lookup
}
}
$item["MAC_x0020_Address"] = $row."MAC Address" -replace "[\W]", ""
$item["Operating_x0020_System_x002f_Ver"] = $row."Operating System/Version"
if($($row."Excess Date").length -gt 1 ){$item["Excess_x0020_Date"] = $row."Excess Date"}
$item["ETID_x0020_Number"] = $row."ETID"
$item["Notes_x002f_Comments"] = $row."Notes"
$item["Maint_x0020_Contract"] = $row."Maintenance Contract" -replace "[\W]", ""
$item["Funding_x0020_Doc"] = $row."Funding Doc"
$item["Purpose"] = $row."Purpose"
$item["Firmware_x0020_Version"] = $row."Firmware Version"
$item["Associated_x0020_Certificate"] = $row."Associated Certificate"
if($($row."Certificate Expiration Date").length -gt 1 ){$item["Certificate_x0020_Expiration_x00"] = $row."Certificate Expiration Date"}
$item["Classification"] = $row."Classification" -replace "[\W]", ""
$item["CLIN"] = $row."CLIN" -replace "[\W]", ""
$item["Color_x0020_Printer"] = $row."Color Printer"
$item["DPAS"] = $row."DPAS"
$item["Criticality"] = $row."Criticality"
$item["FQDN"] = $row."FQDN" -replace "[\W]", ""
$item["Hard_x0020_Drive"] = $row."Hard Drive"
$item["IP_x0020_Address"] = $row."IP Address" -replace "[\W]", ""
$item["Code_x002f_Shop"] = $row."Code/Shop" -replace "[\W]", ""
$item["Monthly_x0020_Fee"] = $row."Monthly Fee" -replace "[\W]", ""
$item["Phone_x0020_Number"] = $row."Phone Number" -replace "[\W]", ""
$item["Badge_x0020_Number"] = $row."Badge Number" -replace "[\W]", ""
$item["Print_x0020_Queue"] = $row."Print Queue" -replace "[\W]", ""
$item["Replacement_x0020_Cost"] = $row."Replacement Cost" -replace "[\W]", ""
$item["Certificate"] = $row."Certificate"
$item["Camera"] = $row."Camera"
$item["Wireless_x0020_MAC_x0020_Address"] = $row."Wireless MAC Address"
$item["RAM"] = $row."RAM"
$item["MAC_x0020_Address_x0020_2"] = $row."MAC Address 2"
$item["Sim_x0020_Card"] = $row."Sim Card"
$item["Connected_x0020_to_x0020_VOIP_x0"] = $row."Connected to VOIP Phone"
$item["VOIP_x0020_Phone_x0020_Number"] = $row."VOIP Phone Number"
$item["Number_x0020_of_x0020_Antennas"] = $row."Number of Antennas"
#$item["Subnet_x0020_Mask"] =
#$item["Gateway"] =
#$item["Network_x0020_Switch"] =
#$item["VLan"] =
#$item["Wall_x0020_Jack"] =
#$item["Zone"] =
try
{
$item.Update()
}
catch
{
$exception = $_.Exception.Message
"Item Number: $i - $exception" | out-file "f:\temp\dataMigration.txt" -append
}
}