Power Shell

Code:

$Path = "C:\Users\xxxx\Downloads\New";
$filename="$Path\c.txt";

#create file if it does not exist
if(-not (Test-Path $filename))
{
    New-Item -ItemType file -Path $filename
}

#write current date to file
$currentDate  = get-date
Add-Content $filename -Value $currentDate

#get files in the folder and assign items to a collection
$files = Get-ChildItem -Path $Path

#iterate item
foreach($item in $files){
    Write-Host $item
}
output:
a.txt
b.txt
c.txt

#another output
$files | ForEach-Object -Process {$_.FullName}
Output:
C:\Users\xxx\Downloads\New\a.txt
C:\Users\xxx\Downloads\New\b.txt
C:\Users\xxx\Downloads\New\c.txt

#get window service
Get-Service * | Sort-Object ServiceType | Format-Table Name, ServiceType, Status -AutoSize
Output:
Name                                                                 ServiceType  Status
----                                                                 -----------  ------
AdobeARMservice                                                  Win32OwnProcess Running
MSOLAP$SQLSERVER2012                                             Win32OwnProcess Running
MSOLAP$BERNIE_SQLSERVER                                          Win32OwnProcess Running

#switch
switch(4,2)
{
    4{"4"}
    3{"3"}
    2{"2"}
    1{"1"}
}
Output:
4
2

posted on 2022-08-31 11:21  傍晚雨  阅读(132)  评论(0编辑  收藏  举报