Advertisement
wetyukmnbxc

Powershell further

Mar 16th, 2023
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. Sure! Here's an example of a more complex PowerShell function that takes two inputs and returns one output:
  2.  
  3. ```PowerShell
  4. function Get-LongestWord ($string1, $string2) {
  5. $words1 = $string1.Split(" ")
  6. $words2 = $string2.Split(" ")
  7. $longestWord = ""
  8.  
  9. foreach ($word in $words1 + $words2) {
  10. if ($word.Length -gt $longestWord.Length) {
  11. $longestWord = $word
  12. }
  13. }
  14.  
  15. return $longestWord
  16. }
  17. ```
  18.  
  19. This function takes two input parameters `$string1` and `$string2`, splits them into words using the `Split` method and assigns the resulting arrays to the variables `$words1` and `$words2`. The function then initializes an empty string variable `$longestWord` to store the longest word.
  20.  
  21. The `foreach` loop iterates over all words in both arrays by concatenating them with the `+` operator. For each word, the loop checks if its length is greater than the length of the current longest word. If it is, the loop assigns the current word to the `$longestWord` variable.
  22.  
  23. After all words have been processed, the `return` statement outputs the value of `$longestWord`.
  24.  
  25. You can call this function like this: `Get-LongestWord "The quick brown fox" "jumps over"` and it will return `"jumps"`.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement