Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1. Example 1: Using .NET classes to manipulate files and directories
- # This script demonstrates how to use .NET classes to manipulate files and directories
- # Create a new directory
- $dirPath = "C:\Temp\NewDirectory"
- [System.IO.Directory]::CreateDirectory($dirPath)
- # Create a new file
- $filePath = "C:\Temp\NewDirectory\NewFile.txt"
- [System.IO.File]::Create($filePath)
- # Write some text to the file
- $fileContent = "This is some text that will be written to the file."
- [System.IO.File]::WriteAllText($filePath, $fileContent)
- # Read the contents of the file
- $fileContent = [System.IO.File]::ReadAllText($filePath)
- Write-Host $fileContent
- # Delete the file and directory
- [System.IO.File]::Delete($filePath)
- [System.IO.Directory]::Delete($dirPath)
- 2. Example 2: Using .NET classes to work with dates and times
- # This script demonstrates how to use .NET classes to work with dates and times
- # Get the current date and time
- $currentDateTime = Get-Date
- # Display the current date and time in various formats
- Write-Host "Current date and time:"
- Write-Host " Long date format: $($currentDateTime.ToLongDateString())"
- Write-Host " Short date format: $($currentDateTime.ToShortDateString())"
- Write-Host " Long time format: $($currentDateTime.ToLongTimeString())"
- Write-Host " Short time format: $($currentDateTime.ToShortTimeString())"
- # Add 10 days to the current date
- $newDateTime = $currentDateTime.AddDays(10)
- # Display the new date in various formats
- Write-Host "New date:"
- Write-Host " Long date format: $($newDateTime.ToLongDateString())"
- Write-Host " Short date format: $($newDateTime.ToShortDateString())"
- 3. Example 3: Using .NET classes to work with regular expressions
- # This script demonstrates how to use .NET classes to work with regular expressions
- # Define a regular expression pattern
- $pattern = "\d+"
- # Define some test strings
- $testStrings = @("1234", "abcd", "5678", "!@#$")
- foreach ($string in $testStrings) {
- # Test if the string matches the pattern
- if ([System.Text.RegularExpressions.Regex]::IsMatch($string, $pattern)) {
- Write-Host "$string matches pattern."
- } else {
- Write-Host "$string does not match pattern."
- }
- }
- # Extract all numbers from a string using regular expressions
- $stringWithNumbers = "abc123def456ghi789jkl"
- $numbersOnlyPattern = "\d+"
- $matches = [System.Text.RegularExpressions.Regex]::Matches($stringWithNumbers, $numbersOnlyPattern)
- foreach ($match in $matches) {
- Write-Host $match.Value
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement