Hello,
I have a CSV file that contains a list of Windows operating systems that need to be updated often using new folder names created during the update process.
For example, when Windows 11 Pro x64 21H2 22000.318
is updated offline, the updated version is placed into a new folder. That new folder name will either have an updated Build number, or a timestamp appended to the original name automatically: For example: Windows 11 Pro x64 21H2 22000.556
or Windows 11 Pro x64 21H2 22000.556 2203140144
.
I was able to figure out how to grab those new folder names, and sort them in order before plugging them into the CSV file using the following code:
### Update CSV with new folder names ###
# Global Variables
$OSMedia = "D:\OSD\Builder\OSMedia\"
$csv_file = "D:\OSD\Builder\List-SourceNames.csv"
$csv_file_old = "D:\OSD\Builder\List-SourceNames-OLD.csv"
# Backup CSV File
Copy-Item $csv_file -Destination $csv_file_old -Force
# Prompt for latest number of folders to get
$num = Read-Host "Enter number of latest folders"
# Create $latest folder names array (excluding "build" folders)
$latest = (Get-ChildItem $OSMedia -Exclude "build*" | Where { $_.PSIsContainer } | Sort CreationTime -Descending | Select -First $num | Select-Object Name).Name
$latest = $latest | Sort-Object
# Replace osd_names in CSV with $latest folder names
$osd_names = Import-CSV "$csv_file_new"
$num = 0
foreach ($osd_name in $osd_names)
{
$osd_name.osd_name = $osd_name.osd_name.replace($osd_name.osd_name,$latest[$num])
$num = $num+1
}
$osd_names | Export-CSV "$csv_file" -Force -NoTypeInformation -Encoding ASCII
Iām currently working with six operating systems. If all six are updated, then using my above script will work perfectly. However, if I have fewer than six, I need to update the CSV file manually because the above script doesnāt know how to tell a given value to only replace another given value.
Iām wondering if I could use the first part of the folder names that never change, such as Windows 11 Pro x64 21H2
. Based on this value, the script would (hypothetically) know to replace Windows 11 Pro x64 21H2 22000.318
with Windows 11 Pro x64 21H2 22000.556
.
CSV column contents:
osd_name
Windows 10 Enterprise LTSC 2021 x64 21H2 19044.1566
Windows 10 Pro for Workstations x64 21H2 19044.1288
Windows 11 Pro for Workstations x64 21H2 22000.318
Windows 11 Pro x64 21H2 22000.318
Windows Server 2022 Datacenter Desktop Experience x64 Dev 22509.1000
Windows Server 2022 Datacenter x64 Dev 22509.1000
Iām playing around with the -split
option, but Iām not sure this will help me achieve my objective.
$split_names = "Windows 11 Pro x64 21H2 22000.556"
$a = $split_names.split(".")[0]
$a
Output: Windows 11 Pro x64 21H2 22000
Is there a way that I can use a portion of this string Windows 11 Pro x64 21H2 22000.318
or another value in my csv file to tell PowerShell what value to replace it with?
Old Name: Windows 11 Pro x64 21H2 22000.318
New Name: Windows 11 Pro x64 21H2 22000.556
I have a column that I use for the base name for each OS, such as Windows 11 Pro
.
Could someone point me in the right direction so I can take a stab at it?
Thank youā¦