Advertisement
wetyukmnbxc

Simple - Give 3 example powershell scripts that shows "how to work with .NET Classes"

Mar 21st, 2023
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 1. Example 1: Using .NET classes to manipulate files and directories
  2.  
  3. # This script demonstrates how to use .NET classes to manipulate files and directories
  4.  
  5. # Create a new directory
  6. $dirPath = "C:\Temp\NewDirectory"
  7. [System.IO.Directory]::CreateDirectory($dirPath)
  8.  
  9. # Create a new file
  10. $filePath = "C:\Temp\NewDirectory\NewFile.txt"
  11. [System.IO.File]::Create($filePath)
  12.  
  13. # Write some text to the file
  14. $fileContent = "This is some text that will be written to the file."
  15. [System.IO.File]::WriteAllText($filePath, $fileContent)
  16.  
  17. # Read the contents of the file
  18. $fileContent = [System.IO.File]::ReadAllText($filePath)
  19. Write-Host $fileContent
  20.  
  21. # Delete the file and directory
  22. [System.IO.File]::Delete($filePath)
  23. [System.IO.Directory]::Delete($dirPath)
  24.  
  25.  
  26. 2. Example 2: Using .NET classes to work with dates and times
  27.  
  28. # This script demonstrates how to use .NET classes to work with dates and times
  29.  
  30. # Get the current date and time
  31. $currentDateTime = Get-Date
  32.  
  33. # Display the current date and time in various formats
  34. Write-Host "Current date and time:"
  35. Write-Host "   Long date format: $($currentDateTime.ToLongDateString())"
  36. Write-Host "   Short date format: $($currentDateTime.ToShortDateString())"
  37. Write-Host "   Long time format: $($currentDateTime.ToLongTimeString())"
  38. Write-Host "   Short time format: $($currentDateTime.ToShortTimeString())"
  39.  
  40. # Add 10 days to the current date
  41. $newDateTime = $currentDateTime.AddDays(10)
  42.  
  43. # Display the new date in various formats
  44. Write-Host "New date:"
  45. Write-Host "   Long date format: $($newDateTime.ToLongDateString())"
  46. Write-Host "   Short date format: $($newDateTime.ToShortDateString())"
  47.  
  48.  
  49. 3. Example 3: Using .NET classes to work with regular expressions
  50.  
  51. # This script demonstrates how to use .NET classes to work with regular expressions
  52.  
  53. # Define a regular expression pattern
  54. $pattern = "\d+"
  55.  
  56. # Define some test strings
  57. $testStrings = @("1234", "abcd", "5678", "!@#$")
  58.  
  59. foreach ($string in $testStrings) {
  60.     # Test if the string matches the pattern
  61.     if ([System.Text.RegularExpressions.Regex]::IsMatch($string, $pattern)) {
  62.         Write-Host "$string matches pattern."
  63.     } else {
  64.         Write-Host "$string does not match pattern."
  65.     }
  66. }
  67.  
  68. # Extract all numbers from a string using regular expressions
  69. $stringWithNumbers = "abc123def456ghi789jkl"
  70. $numbersOnlyPattern = "\d+"
  71. $matches = [System.Text.RegularExpressions.Regex]::Matches($stringWithNumbers, $numbersOnlyPattern)
  72. foreach ($match in $matches) {
  73.     Write-Host $match.Value
  74. }
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement