PowerShell: Select custom expression and expand in one operation - powershell
I can do something like
ls . `
| select #{ Name="Dir"; Expression = { $_ | Split-Path } } `
| select -ExpandProperty Dir
to select a custom expression (in this case $_ | Split-Path) into a simple array of values.
Is there a way to merge the two select statements into one, that still yields equivalent results?
I think the OP is looking for ForEach-Object (MSDN), which performs an operation against each item in a collection of input objects. It's similar to the map or Select operation in other languages.
So instead of:
ls . `
| select #{ Name="Dir"; Expression = { $_ | Split-Path } } `
| select -ExpandProperty Dir
You want:
ls . `
| ForEach-Object { $_ | Split-Path }
You can also replace ForEach-Object with %. But it's less readable and less searchable for new PowerShell users:
ls . `
| % { $_ | Split-Path }
What are you trying to do? You could get the same results with just ls/Get-ChildItem and Split-Path. Not sure if you need the Microsoft.PowerShell.Core\FileSystem:: part.
PS C:\Path\To\Dir> Get-ChildItem | Split-Path
Microsoft.PowerShell.Core\FileSystem::C:\Path\To\Dir
Microsoft.PowerShell.Core\FileSystem::C:\Path\To\Dir
Microsoft.PowerShell.Core\FileSystem::C:\Path\To\Dir
PS C:\Path\To\Dir> (Get-ChildItem).FullName | Split-Path
C:\Path\To\Dir
C:\Path\To\Dir
C:\Path\To\Dir
Edit
Assuming nuget list <my-package> outputs a string array, you can use -replace and regex ($ to match string end) to manipulate it and get an array of results.
$((nuget list <my-package>).Replace(" ",",") -replace "$",".nupkg")
Related
Count number of comments over multiple files, including multi-line comments
I'm trying to write a script that counts all comments in multiple files, including both single line (//) and multi-line (/* */) comments and prints out the total. So, the following file would return 4 // Foo var text = "hello world"; /* Bar */ alert(text); There's a requirement to include specific file types and exclude certain file types and folders, which I already have working in my code. My current code is: ( gci -include *.cs,*.aspx,*.js,*.css,*.master,*.html -exclude *.designer.cs,jquery* -recurse ` | ? { $_.FullName -inotmatch '\\obj' } ` | ? { $_.FullName -inotmatch '\\packages' } ` | ? { $_.FullName -inotmatch '\\release' } ` | ? { $_.FullName -inotmatch '\\debug' } ` | ? { $_.FullName -inotmatch '\\plugin-.*' } ` | select-string "^\s*//" ` ).Count How do I change this to get multi-line comments as well? UPDATE: My final solution (slightly more robust than what I was asking for) is as follows: $CodeFiles = Get-ChildItem -include *.cs,*.aspx,*.js,*.css,*.master,*.html -exclude *.designer.cs,jquery* -recurse | Where-Object { $_.FullName -notmatch '\\(obj|packages|release|debug|plugin-.*)\\' } $TotalFiles = $CodeFiles.Count $IndividualResults = #() $CommentLines = ($CodeFiles | ForEach-Object{ #Get the comments via regex $Comments = ([regex]::matches( [IO.File]::ReadAllText($_.FullName), '(?sm)^[ \t]*(//[^\n]*|/[*].*?[*]/)' ).Value -split '\r?\n') | Where-Object { $_.length -gt 0 } #Get the total lines $Total = ($_ | select-string .).Count #Add to the results table $IndividualResults += #{ File = $_.FullName | Resolve-Path -Relative; Comments = $Comments.Count; Code = ($Total - $Comments.Count) Total = $Total } Write-Output $Comments }).Count $TotalLines = ($CodeFiles | select-string .).Count $TotalResults = New-Object PSObject -Property #{ Files = $TotalFiles Code = $TotalLines - $CommentLines Comments = $CommentLines Total = $TotalLines } Write-Output (Get-Location) Write-Output $IndividualResults | % { new-object PSObject -Property $_} | Format-Table File,Code,Comments,Total Write-Output $TotalResults | Format-Table Files,Code,Comments,Total
To be clear: Using string matching / regular expressions is not a fully robust way to detect comments in JavaScript / C# code, because there can be false positives (e.g., var s = "/* hi */";); for robust parsing you'd need a language parser. If that is not a concern, and it is sufficient to detect comments (that start) on their own line, optionally preceded by whitespace, here's a concise solution (PSv3+): (Get-ChildItem -include *.cs,*.aspx,*.js,*.css,*.master,*.html -exclude *.designer.cs,jquery* -recurse | Where-Object { $_.FullName -notmatch '\\(obj|packages|release|debug|plugin-.*)' } | ForEach-Object { [regex]::matches( [IO.File]::ReadAllText($_.FullName), '(?sm)^[ \t]*(//[^\n]*|/[*].*?[*]/)' ).Value -split '\r?\n' } ).Count With the sample input, the ForEach-Object command yields 4. Remove the ^[ \t]* part to match comments starting anywhere on a line. The solution reads each input file as a single string with [IO.File]::ReadAllText() and then uses the [regex]::Matches() method to extract all (potentially line-spanning) comments. Note: You could use Get-Content -Raw instead to read the file as a single string, but that is much slower, especially when processing multiple files. The regex uses in-line options s and m ((?sm)) to respectively make . match newlines too and to make anchors ^ and $ match line-individually. ^[ \t]* matches any mix of spaces and tabs, if any, at the start of a line. //[^\n]*$ matches a string that starts with // through the end of the line. /[*].*?[*]/ matches a block comment across multiple lines; note the lazy quantifier, *?, which ensures that very next instance of the closing */ delimiter is matched. The matched comments (.Value) are then split into individual lines (-split '\r?\n'), which are output. The resulting lines across all files are then counted (.Count) As for what you tried: The fundamental problem with your approach is that Select-String with file-info object input (such as provided by Get-ChildItem) invariably processes the input files line by line. While this could be remedied by calling Select-String inside a ForEach-Object script block in which you pass each file's content as a single string to Select-String, direct use of the underlying regex .NET types, as shown above, is more efficient.
An IMO better approach is to count net code lines by removing single/multi line comments. For a start a script that handles single files and returns for your above sample.cs the result 5 ((Get-Content sample.cs -raw) -replace "(?sm)^\s*\/\/.*?$" ` -replace "(?sm)\/\*.*?\*\/.*`n" | Measure-Object -Line).Lines EDIT: without removing empty lines, build the difference from total lines ## Q:\Test\2018\10\31\SO_53092258.ps1 $Data = Get-ChildItem *.cs | ForEach-Object { $Content = Get-Content $_.FullName -Raw $TotalLines = (Measure-Object -Input $Content -Line).Lines $CodeLines = ($Content -replace "(?sm)^\s*\/\/.*?$" ` -replace "(?sm)\/\*.*?\*\/.*`n" | Measure-Object -Line).Lines $Comments = $TotalLines - $CodeLines [PSCustomObject]#{ File = $_.FullName Lines = $TotalLines Comments= $Comments } } $Data "="*40 "TotalLines={0} TotalCommentLines={1}" -f ( $Data | Measure-Object -Property Lines,Comments -Sum).Sum Sample output: > Q:\Test\2018\10\31\SO_53092258.ps1 File Lines Comments ---- ----- -------- Q:\Test\2018\10\31\example.cs 10 5 Q:\Test\2018\10\31\sample.cs 9 4 ============================================ TotalLines=19 TotalCommentLines=9
Searching for key in ConvertFrom-StringData hash
I have a hash table that I got from a file, command used: [array]$hash= Get-Content -raw '../../file.txt' | ConvertFrom-StringData file.txt looks like this: key=value It works perfectly, the problem is when I trying to search using $hash.GetEnumerator. I am trying to do something like this: $hash.GetEnumerator() | where {$_.value -match 'value'} // or with key It always returns an empty value. Got it from link, tried to create a local hash using $hash=#{} then add, and it worked(like for the guy from the link). Note! $hash.GetEnumerator() | Sort-Object Name works for me too, and returning the right table. Do you have any idea how can I search(-eq or -match) in the hash table that I have created?
try this $hash.GetEnumerator() | where {$($_.Value) -match 'value'} or like this $hash.Keys | % {$($hash[$_]) -eq 'value'} | %{$hash[$_]} or better, you can transform your hashtable into objects list $hash.GetEnumerator() | % {New-Object psobject -Property #{Name=$($_.Name); Value=$($_.Value)} } | where Value -match 'value'
Your response to my comment asking for clarification didn't clarify too much, but I'm going to assume that you want to find the value for a particular key in the hashtable. That kind of lookup is the core functionality of hashtables. $ht = #' foo=23 bar=42 baz=5 '# | ConvertFrom-StringData $key = 'bar' $ht[$key] # returns 42 If you actually want a fuzzy match on the keys you could replace the direct lookup with a wildcard match like this: $partialKey = 'ba*' $ht.Keys | Where-Object { $_ -like $partialKey } | ForEach-Object { $ht[$_] # returns 42 and 5 } or a regular expression match like this: $partialKey = '^ba' $ht.Keys | Where-Object { $_ -match $partialKey } | ForEach-Object { $ht[$_] # returns 42 and 5 } As an alternative to looking up multiple keys in a loop you could also build a list of keys and use that list in a single lookup: $partialKey = 'ba*' $keys = $ht.Keys | Where-Object { $_ -like $partialKey } $ht[$keys] # returns 42 and 5
Compare-object output in variable
I'm building a script that will compare the last octed of in-use IPv4 addreses, with all the available octeds (2 till 254). I am already this far that I do get a result by comparing array's, but my end-result is also an array, and I cannot seem to get only the number. My script: $guestIP = #("192.168.31.200","192.168.31.31","192.168.31.90","192.168.31.25","192.168.31.100") $AllLastOcted = $("2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49","50","51","52","53","54","55","56","57","58","59","60","61","62","63","64","65","66","67","68","69","70","71","72","73","74","75","76","77","78","79","80","81","82","83","84","85","86","87","88","89","90","91","92","93","94","95","96","97","98","99","100","101","102","103","104","105","106","107","108","109","110","111","112","113","114","115","116","117","118","119","120","121","122","123","124","125","126","127","128","129","130","131","132","133","134","135","136","137","138","139","140","141","142","143","144","145","146","147","148","149","150","151","152","153","154","155","156","157","158","159","160","161","162","163","164","165","166","167","168","169","170","171","172","173","174","175","176","177","178","179","180","181","182","183","184","185","186","187","188","189","190","191","192","193","194","195","196","197","198","199","200","201","202","203","204","205","206","207","208","209","210","211","212","213","214","215","216","217","218","219","220","221","222","223","224","225","226","227","228","229","230","231","232","233","234","235","236","237","238","239","240","241","242","243","244","245","246","247","248","249","250","251","252","253","254") $guestIP = $guestIP | sort -Property {$_-replace '[\d]'},{$_-replace '[a-zA-Z\p{P}]'-as [int]} $AllLastOcted = $AllLastOcted | sort -Property {$_-replace '[\d]'},{$_-replace '[a-zA-Z\p{P}]'-as [int]} $guestIP = $guestIP -replace('192.168.31.','') Compare-Object -ReferenceObject ($AllLastOcted ) -DifferenceObject ($guestIP) | select -Property InputObject -First 1 | set-Variable AvailableOcted -PassThru $AvailableOcted My goal is, is that I have as result the first-available octed that I can use. like: write-host "IP that can be used is 192.168.31.$AvailableOcted" PS > IP that can used is 192.168.31.2
You can simplify this a lot. Instead of defining all numbers from 2 to 254 you can use the range operator to create the array. You also don't need the [int] casts. Instead of using the Compare-Object cmdlet to filter the octeds, you can use the Where-Object cmdlet: $guestIP = #("192.168.31.200","192.168.31.31","192.168.31.90","192.168.31.25","192.168.31.100") $AllLastOcted = 2 .. 254 $usedOcted = $guestIP -replace '.*\.' $nextAvailableOcted = $AllLastOcted | Where { $_ -NotIn $usedOcted } | select -first 1 write-host "IP that can be used is 192.168.31.$nextAvailableOcted" Output: IP that can be used is 192.168.31.2
Well, as simple as: $AvailableOcted.InputObject would return only the value. So it would look like this: write-host ("IP that can be used is 192.168.31." + $AvailableOcted.InputObject)
Variable referencing. How to create arrays getting their names from elements of another array
This is as simplified version of what I'd like to achieve... I think it's called 'variable referencing' I have created an array containing the content of the folder 'foo' $myDirectory(folder1, folder2) Using the following code: $myDirectory= Get-ChildItem ".\foo" | ForEach-Object {$_.BaseName} I'd like to create 2 arrays named as each folders, with the contained files. folder1(file1, file2) folder2(file1, file2, file3) I tried the following code: foreach ($myFolder in $myDirectory) { ${myFolder} = Get-ChildItem ".\$myFolders" | forEach-Object {$_.BaseName} } But obviously didn't work. In bash it's possible create an array giving it a variable's name like this: "${myForder[#]}" I tried to search on Google but I couldn't find how to do this in Powershell
$myDirectory = "c:\temp" Get-ChildItem $myDirectory | Where-Object{$_.PSIsContainer} | ForEach-Object{ Remove-Variable -Name $_.BaseName New-Variable -Name $_.BaseName -Value (Get-ChildItem $_.FullName | Where-Object{!$_.PSIsContainer} | Select -ExpandProperty Name) } I think what you are looking for is New-Variable. Cycle through all the folders under C:\temp. For each folder make a new variable. It would throw errors if the variable already exists. What you could do for that is remove a pre-exising variable. Populate the variable with the current folders contents in the pipeline using Get-ChildItem. The following is a small explanation of how the -Value of the new variable is generated. Caveat Remove-Variable has the potiential to delete unintended variables depending on your folder names. Not sure of the implications of that. Get-ChildItem $_.FullName | Where-Object{!$_.PSIsContainer} | Select -ExpandProperty Name The value of each custom variable is every file ( not folder ). Use -ExpandProperty to just gets the names as strings as supposed to a object with Names. Aside What do you plan on using this data for? It might just be easier to pipe the output from the Get-ChildItem into another cmdlet. Or perhaps create a custom object with the data you desire. Update from comments $myDirectory = "c:\temp" Get-ChildItem $myDirectory | Where-Object{$_.PSIsContainer} | ForEach-Object{ [PSCustomObject] #{ Hotel = $_.BaseName Rooms = (Get-ChildItem $_.FullName | Where-Object{!$_.PSIsContainer} | Select -ExpandProperty Name) } } You need to have at least PowerShell 3.0 for the above to work. Changing it for 2.0 is easy if need be. Create and object with hotel names and "rooms" which are the file names from inside the folder. If you dont want the extension just use BaseName instead of Name in the select.
This is how I did it at the end: # Create an array containing all the folder names $ToursArray = Get-ChildItem -Directory '.\.src\panos' | Foreach-Object {$_.Name} # For each folder... $ToursArray | ForEach-Object { # Remove any variable named as the folder's name. Check if it exists first to avoid errors if(Test-Path variable:$_.BaseName){ Remove-Variable -Name $_.BaseName } $SceneName=Get-ChildItem ".\.src\panos\$_\*.jpg" # Create an array using the main folder's name, containing the names of all the jpg inside New-Variable -Name $_ -Value ($SceneName | Select -ExpandProperty BaseName) } And here it goes some code to check the content of all the arrays: # Print Tours information Write-Verbose "Virtual tours list: ($($ToursArray.count))" $ToursArray | ForEach-Object { Write-Verbose " Name: $_" Write-Verbose " Scenes: $($(Get-Variable $_).Value)" } Output: VERBOSE: Name: tour1 VERBOSE: Scenes: scene1 scene2 VERBOSE: Name: tour2 VERBOSE: Scenes: scene1
How do I get directory depth in PowerShell 3.0?
I need to find out how far down the directory structure inside a working directory goes. If the layout is something like Books\ Email\ Notes\ Note 1.txt Note 2.txt HW.docx then it should return 1, because the deepest items are 1 level below. But if it looks like Books\ Photos\ Hello.c then it should return 0, because there is nothing deeper than the first level.
Something like this should do the trick in V3: Get-ChildItem . -Recurse -Name | Foreach {($_.ToCharArray() | Where {$_ -eq '\'} | Measure).Count} | Measure -Maximum | Foreach Maximum
It's not as pretty, and arguably not as "Posh" as Keith's, but I suspect it might scale better. $depth_ht = #{} (cmd /c dir /ad /s) -replace '[^\\]','' | foreach {$depth_ht[$_]++} $max_depth = $depth_ht.keys | sort length | select -last 1 | select -ExpandProperty length $root_depth = ($PWD -replace '[^\\]','').length ($max_depth -$root_depth)