{"id":20166,"date":"2026-02-03T16:37:10","date_gmt":"2026-02-03T20:37:10","guid":{"rendered":"https:\/\/nycphantom.com\/journal\/?p=20166"},"modified":"2026-02-03T16:58:53","modified_gmt":"2026-02-03T20:58:53","slug":"my-first-coding-using-ai-matching-one-to-many-list-of-names-from-one-excel-spreadsheet-with-another-spreadsheet-and-update-its-rows-accordingly","status":"publish","type":"post","link":"http:\/\/nycphantom.com\/journal\/?p=20166","title":{"rendered":"My first coding (powershell scripting) using AI: Matching (one to many) list of names from one Excel Spreadsheet with another Spreadsheet and update its rows accordingly"},"content":{"rendered":"\n<p>To streamline a routine work at SPG, I used Microsoft Copilot for this. This was the prompt I used:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>write a powershell script that takes a list from an excel sheet column, and match every value on that list with another excel sheet to update some values of it in the matching rows.<\/code><\/pre>\n\n\n\n<p>I was asked to install a new module: <em>Install-Module ImportExcel -Scope CurrentUser<\/em><\/p>\n\n\n\n<p>PS: This doesn't update anything on sharepoint. So I actually would need to copy the sharepoint file to PC to be the target file and then I copy paste the PowerShell updated column from the local file to the sharepoint file (not replacing the entire file).<\/p>\n\n\n\n<p>And I got this code from M365 Copilot Chat:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n#Requires -Modules ImportExcel\n\nparam(\n    &#91;Parameter(Mandatory)] &#91;string] $ListFile,\n    &#91;Parameter(Mandatory)] &#91;string] $ListSheet,\n    &#91;Parameter(Mandatory)] &#91;string] $ListKeyColumn,\n\n    &#91;Parameter(Mandatory)] &#91;string] $TargetFile,\n    &#91;Parameter(Mandatory)] &#91;string] $TargetSheet,\n    &#91;Parameter(Mandatory)] &#91;string] $TargetKeyColumn\n)\n\n# ---- Settings: columns to update in the target (constants) ----\n$ConstantUpdates = @{\n    \"Status\"    = \"Disabled\"\n    \"UpdatedOn\" = (Get-Date).ToString(\"yyyy-MM-dd\")\n}\n\n# ---- Safety: create a backup first ----\n$backup = \"$TargetFile.bak_{0:yyyyMMdd_HHmmss}\" -f (Get-Date)\nCopy-Item $TargetFile $backup -Force\nWrite-Host \"Backup created: $backup\"\n\n# ---- Read list keys ----\n$listRows = Import-Excel -Path $ListFile -WorksheetName $ListSheet\nif (-not $listRows) { throw \"List sheet returned no rows.\" }\n\n$keys = $listRows |\n    ForEach-Object { $_.$ListKeyColumn } |\n    Where-Object { $_ -ne $null -and \"$_\".Trim() -ne \"\" } |\n    ForEach-Object { \"$_\".Trim() } |\n    Select-Object -Unique\n\nif ($keys.Count -eq 0) { throw \"No keys found in column '$ListKeyColumn'.\" }\n\n# Build a fast lookup set (case-insensitive by default)\n$keySet = &#91;System.Collections.Generic.HashSet&#91;string]]::new(&#91;System.StringComparer]::OrdinalIgnoreCase)\n$null = $keys | ForEach-Object { $keySet.Add($_) }\n\n# ---- Open target workbook and update in place ----\n$pkg = Open-ExcelPackage -Path $TargetFile\ntry {\n    $ws = $pkg.Workbook.Worksheets&#91;$TargetSheet]\n    if (-not $ws) { throw \"Target sheet '$TargetSheet' not found in '$TargetFile'.\" }\n\n    $endRow = $ws.Dimension.End.Row\n    $endCol = $ws.Dimension.End.Column\n\n    # Map headers (row 1) to column indexes\n    $headerToCol = @{}\n    for ($c = 1; $c -le $endCol; $c++) {\n        $h = $ws.Cells&#91;1, $c].Text.Trim()\n        if ($h) { $headerToCol&#91;$h] = $c }\n    }\n\n    if (-not $headerToCol.ContainsKey($TargetKeyColumn)) {\n        throw \"Target key column '$TargetKeyColumn' not found in header row.\"\n    }\n\n    # Ensure update columns exist\n    foreach ($colName in $ConstantUpdates.Keys) {\n        if (-not $headerToCol.ContainsKey($colName)) {\n            throw \"Update column '$colName' not found in target header row.\"\n        }\n    }\n\n    $keyColIndex = $headerToCol&#91;$TargetKeyColumn]\n\n    $matched = 0\n    $updated = 0\n\n    for ($r = 2; $r -le $endRow; $r++) {\n        $key = $ws.Cells&#91;$r, $keyColIndex].Text.Trim()\n        if (-not $key) { continue }\n\n        if ($keySet.Contains($key)) {\n            $matched++\n\n            foreach ($colName in $ConstantUpdates.Keys) {\n                $colIndex = $headerToCol&#91;$colName]\n                $newValue = $ConstantUpdates&#91;$colName]\n\n                # Only write if different\n                $current = $ws.Cells&#91;$r, $colIndex].Text\n                if ($current -ne \"$newValue\") {\n                    $ws.Cells&#91;$r, $colIndex].Value = $newValue\n                    $updated++\n                }\n            }\n        }\n    }\n\n    Close-ExcelPackage -ExcelPackage $pkg -Save\n\n    Write-Host \"Matches found: $matched\"\n    Write-Host \"Cells updated: $updated\"\n}\ncatch {\n    # If anything fails, close without saving to avoid partial writes\n    Close-ExcelPackage -ExcelPackage $pkg -NoSave\n    throw\n}\n<\/code><\/pre>\n\n\n\n<p>And I only needed to made a few modifications:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n#Requires -Modules ImportExcel\n\n$ListFile = \"C:\\Users\\original.xlsx\"\n$ListSheet = \"sheet1\"\n$ListKeyColumn = \"User principal name\"\n\n$TargetFile = \"C:\\Users\\target.xlsx\"\n$TargetSheet = \"Users\"\n$TargetKeyColumn = \"Email\"\n\n\n\n# ---- Settings: columns to update in the target (constants) ----\n$ConstantUpdates = @{\n    # \"Status\"    = \"Disabled\"\n    \"DOT\" = (Get-Date).ToString(\"d\")\n}\n\n# ---- Safety: create a backup first ----\n#$backup = \"$TargetFile.bak_{0:yyyyMMdd_HHmmss}\" -f (Get-Date)\n#Copy-Item $TargetFile $backup -Force\n#Write-Host \"Backup created: $backup\"\n\n# ---- Read list keys ----\n$listRows = Import-Excel -Path $ListFile -WorksheetName $ListSheet\nif (-not $listRows) { throw \"List sheet returned no rows.\" }\n\n$keys = $listRows |\n    ForEach-Object { $_.$ListKeyColumn } |\n    Where-Object { $_ -ne $null -and \"$_\".Trim() -ne \"\" } |\n    ForEach-Object { \"$_\".Trim() } |\n    Select-Object -Unique\n\nif ($keys.Count -eq 0) { throw \"No keys found in column '$ListKeyColumn'.\" }\n\n# Build a fast lookup set (case-insensitive by default)\n$keySet = &#91;System.Collections.Generic.HashSet&#91;string]]::new(&#91;System.StringComparer]::OrdinalIgnoreCase)\n$null = $keys | ForEach-Object { $keySet.Add($_) }\n\n# ---- Open target workbook and update in place ----\n$pkg = Open-ExcelPackage -Path $TargetFile\ntry {\n    $ws = $pkg.Workbook.Worksheets&#91;$TargetSheet]\n    if (-not $ws) { throw \"Target sheet '$TargetSheet' not found in '$TargetFile'.\" }\n\n    $endRow = $ws.Dimension.End.Row\n    $endCol = $ws.Dimension.End.Column\n\n    # Map headers (row 1) to column indexes\n    $headerToCol = @{}\n    for ($c = 1; $c -le $endCol; $c++) {\n        $h = $ws.Cells&#91;1, $c].Text.Trim()\n        if ($h) { $headerToCol&#91;$h] = $c }\n    }\n\n    if (-not $headerToCol.ContainsKey($TargetKeyColumn)) {\n        throw \"Target key column '$TargetKeyColumn' not found in header row.\"\n    }\n\n    # Ensure update columns exist\n    foreach ($colName in $ConstantUpdates.Keys) {\n        if (-not $headerToCol.ContainsKey($colName)) {\n            throw \"Update column '$colName' not found in target header row.\"\n        }\n    }\n\n    $keyColIndex = $headerToCol&#91;$TargetKeyColumn]\n\n    $matched = 0\n    $updated = 0\n\n    for ($r = 2; $r -le $endRow; $r++) {\n        $key = $ws.Cells&#91;$r, $keyColIndex].Text.Trim()\n        if (-not $key) { continue }\n\n        if ($keySet.Contains($key)) {\n            $matched++\n\n            foreach ($colName in $ConstantUpdates.Keys) {\n                $colIndex = $headerToCol&#91;$colName]\n                $newValue = $ConstantUpdates&#91;$colName]\n\n                # Only write if different\n                $current = $ws.Cells&#91;$r, $colIndex].Text\n                if (&#91;string]::IsNullOrWhiteSpace($current) -and ($current -ne \"$newValue\")) {\n                    $ws.Cells&#91;$r, $colIndex].Value = &#91;datetime]($newValue)\n                    $ws.Cells&#91;$r, $colIndex].Style.HorizontalAlignment = \"Right\"\n                    $updated++\n                }\n            }\n        }\n    }\n\n    Close-ExcelPackage -ExcelPackage $pkg\n\n    Write-Host \"Matches found: $matched\"\n    Write-Host \"Cells updated: $updated\"\n}\ncatch {\n    # If anything fails, close without saving to avoid partial writes\n    Close-ExcelPackage -ExcelPackage $pkg -NoSave\n    throw\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>To streamline a routine work at SPG, I used Microsoft Copilot for this. This was the prompt I used: I was asked to install a new module: Install-Module ImportExcel -Scope CurrentUser PS: This doesn't update anything on sharepoint. So I &hellip; <a href=\"http:\/\/nycphantom.com\/journal\/?p=20166\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[],"class_list":["post-20166","post","type-post","status-publish","format-standard","hentry","category-computer-science"],"_links":{"self":[{"href":"http:\/\/nycphantom.com\/journal\/index.php?rest_route=\/wp\/v2\/posts\/20166","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/nycphantom.com\/journal\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/nycphantom.com\/journal\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/nycphantom.com\/journal\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/nycphantom.com\/journal\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=20166"}],"version-history":[{"count":3,"href":"http:\/\/nycphantom.com\/journal\/index.php?rest_route=\/wp\/v2\/posts\/20166\/revisions"}],"predecessor-version":[{"id":20171,"href":"http:\/\/nycphantom.com\/journal\/index.php?rest_route=\/wp\/v2\/posts\/20166\/revisions\/20171"}],"wp:attachment":[{"href":"http:\/\/nycphantom.com\/journal\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=20166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/nycphantom.com\/journal\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=20166"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/nycphantom.com\/journal\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=20166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}