如何在PowerShell中手动导出和导入CSV文件?

在PowerShell中运行命令Export-Csv时,它会自动将CSV文件创建到特定位置。

示例

Get-Service | Select Name, Starttype, Status | Export-Csv C:\temp\services.csv -NoTypeInformation

在上面的示例中,在C:\ Temp文件夹中创建的Services.csv文件具有标题Name,StarttypeStatus标题。

同样,您可以使用其他命令将输出转换为CSV文件格式。

如果要从PowerShell控制台检查此文件,则需要使用Import-CSV命令。

Import-Csv C:\temp\Services.csv | ft -Autosize

输出结果

Name                                     StartType       Status
----                                     ---------       ------
AarSvc_7a82a                             Manual          Stopped
AdobeARMservice                          Automatic       Running
AdobeFlashPlayerUpdateSvc                Manual          Stopped
AJRouter                                 Manual          Stopped
ALG                                      Manual          Stopped
AppIDSvc                                 Manual          Stopped
Appinfo                                  Manual          Running
AppMgmt                                  Manual          Stopped
AppReadiness                             Manual          Stopped
AppVClient                               Disabled        Stopped
AppXSvc                                  Manual          Stopped
AssignedAccessManagerSvc                 Manual          Stopped
AudioEndpointBuilder                    Automatic        Running

如果只需要名称和状态,则可以在管道之后使用Select命令

Import-Csv C:\temp\Services.csv | Select Name,Status

输出结果

Name                                        Status
----                                        ------
AarSvc_7a82a                                Stopped
AdobeARMservice                             Running
AdobeFlashPlayerUpdateSvc                   Stopped
AJRouter                                    Stopped
ALG                                         Stopped
AppIDSvc                                    Stopped
Appinfo                                     Running
AppMgmt                                     Stopped
AppReadiness                                Stopped
AppVClient                                  Stopped
AppXSvc                                     Stopped