如何使用Powershell读取Excel

Cat*_*ine 3 windows script powershell shell-script .bash-profile

如何从Excel中提取数据,以便在第一列中包含机器的详细信息,在第二列中包含多个用户名,并且它读取Excel,就像它将通过在第1列中写入的机器名称删除第2列中的所有用户。我的脚本尝试过-

全部清除

工作表名称 = @()

$excel=new-object -com excel.application
$wb=$excel.workbooks.open("c:\users\administrator\my_test.xls")
for ($i=1; $i -le $wb.sheets.count; $i++)
{
  $sheetname+=$wb.Sheets.Item($i).Name;
}
Run Code Online (Sandbox Code Playgroud)

但无法以我想要运行的格式读取。

Cpt*_*ale 7

在Powershell中,您可以使用greatImportExcel模块来处理各种excel数据:

# First-time setup, install the module:
Install-Module ImportExcel

# Import the module:
Import-Module ImportExcel

# Now, import the data from your existing excel document:
$Sheet = Import-Excel -Path 'c:\folder\file.xlsx' -WorksheetName Sheet1

# Display the data by using the column names:
$Sheet | Select 'Computername','Username'
Run Code Online (Sandbox Code Playgroud)

您没有说明您的数据是什么样子,或者您想用它做什么,但这里有一个示例:

# List the data, except the users for certain computername values
$Filtered = $Sheet | Where Computername -NotLike "*BadComputer*" |
  Select Computername,User

# Export the manipulated data back to a new excel sheet
$Filtered | Export-Excel -Path 'c:\folder\file.xlsx' -WorksheetName FilteredSheet
Run Code Online (Sandbox Code Playgroud)