$path = 'c:\windows\explorer.exe' # any item type Test-Path-Path$path # just files Test-Path-Path$path-PathType Leaf # just folders Test-Path-Path$path-PathType Container
PS C:\> Get-Help -Name Get-Date -Parameter Format
-Format []
Displays the date and time in the Microsoft .NET Framework format indicated by the
format specifier. Enter a format specifier. For a list of available format
specifiers, see DateTimeFormatInfo Class
(http://msdn.microsoft.com/library/system.globalization.datetimeformatinfo.aspx)
in MSDN.
When you use the Format parameter, Windows PowerShell gets only the properties of
the DateTime object that it needs to display the date in the format that you
specify. As a result, some of the properties and methods of DateTime objects might
not be available.
Starting in Windows PowerShell 5.0, you can use the following additional formats
as values for the Format parameter.
-- FileDate. A file or path-friendly representation of the current date in local
time. It is in the form of yyyymmdd ( using 4 digits, 2 digits, and 2 digits). An
example of results when you use this format is 20150302.
-- FileDateUniversal. A file or path-friendly representation of the current date
in universal time. It is in the form of yyyymmdd + 'Z' (using 4 digits, 2 digits,
and 2 digits). An example of results when you use this format is 20150302Z.
-- FileDateTime. A file or path-friendly representation of the current date and
time in local time, in 24-hour format. It is in the form of yyyymmdd + 'T' +
hhmmssmsms, where msms is a four-character representation of milliseconds. An
example of results when you use this format is 20150302T1240514987.
-- FileDateTimeUniversal. A file or path-friendly representation of the current
date and time in universal time, in 24-hour format. It is in the form of yyyymmdd
+ 'T' + hhmmssmsms, where msms is a four-character representation of milliseconds,
+ 'Z'. An example of results when you use this format is 20150302T0840539947Z.
Required? false
Position? named
Default value none
Accept pipeline input? false
Accept wildcard characters? false
通过这些信息,您现在可以知道如何格式化日期和时间:
1 2 3
$date = Read-Host-Prompt'Enter a date' $weekday = Get-Date-Date$date-Format'dddd' "$date is a $weekday"
# open destination folder (and create it if needed) $folder = 'c:\drawings' $exists = Test-Path-Path$folder if (!$exists) { $null = New-Item-Path$folder-ItemType Directory } explorer $folder
# walk all 33 web pages that www.metabene.de offers 1..33 | ForEach-Object { $url = "http://www.metabene.de/galerie/page/$_"
# navigate to website... $webpage = Invoke-WebRequest-Uri$url-UseBasicParsing
# take sources of all images on this website... $webpage.Images.src | Where-Object { # take only images that were uploaded to this blog $_-like'*/uploads/*' } } | ForEach-Object { # get filename of URL $filename = $_.Split('/')[-1] # create local file name $destination= Join-Path-Path$Folder-ChildPath$filename # download pictures Invoke-WebRequest-Uri$url-OutFile$destination }
# get invalid characters and escape them for use with RegEx $illegal =[Regex]::Escape(-join [System.Io.Path]::GetInvalidFileNameChars()) $pattern = "[$illegal]"
# get invalid characters and escape them for use with RegEx $illegal =[Regex]::Escape(-join [System.Io.Path]::GetInvalidPathChars()) $pattern = "[$illegal]"