Posts

Showing posts with the label Powershell

Bulk Crop Images From CMD/PowerShell

Answer : ImageMagick is a light-weight tool that can be used for this. Here is an example that croppes all jpg images in a directory and puts the results in a new folder: cd path/to/dir/ mogrify -crop +100+10 -quality 100 -path ../cropped *.jpg Here, 100 pixels from the left border and 10 pixels from the top are removed. See here for more information on how to use crop. Have you tried XnCovert? XnConvert is free cross-platform batch image processor, allowing you to combine over 80 actions. Compatible with 500 formats. It uses the batch processing module of XnViewMP and it is freeware and you can donate them if you find it useful. https://www.xnview.com/en/xnconvert/

Boolean Literals In PowerShell

Answer : $true and $false . Those are constants, though. There are no language-level literals for booleans. Depending on where you need them, you can also use anything that coerces to a boolean value, if the type has to be boolean, e.g. in method calls that require boolean (and have no conflicting overload), or conditional statements. Most non-null objects are true, for example. null , empty strings, empty arrays and the number 0 are false. [bool]1 and [bool]0 also works. To add more information to already existing answers : The boolean literals $true and $false also work as is when used as command line parameters for PowerShell (PS) scripts. For the below PS script which is stored in a file named installmyapp.ps1 : param ( [bool]$cleanuprequired ) echo "Batch file starting execution." Now if I've to invoke this PS file from a PS command line, this is how I can do it: installmyapp.ps1 -cleanuprequired $true OR installmyapp.ps1 -cleanuprequir...

Automate Process Of Disk Cleanup Cleanmgr.exe Without User Intervention

Answer : The following Powershell script automates CleanMgr.exe. In this case, it removes temporary files and runs the Update Cleanup extension to purge superseded Service Pack Backup files (Windows 10 now does this automatically via a scheduled task). To automate other extensions, create a "StateFlags0001" property in the corresponding Registry key, as done in the New-ItemProperty lines. You will find the Registry key names in the "VolumeCaches" branch. As far as being silent, this script attempts to start CleanMgr.exe in a hidden window. However, at some point CleanMgr spawns new processes which are visible and must be waited on separately. Write-Host 'Clearing CleanMgr.exe automation settings.' Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\*' -Name StateFlags0001 -ErrorAction SilentlyContinue | Remove-ItemProperty -Name StateFlags0001 -ErrorAction SilentlyContinue Write-Host 'Enabling Upd...

Change Directory In PowerShell

Image
Answer : Unlike the CMD.EXE CHDIR or CD command, the PowerShell Set-Location cmdlet will change drive and directory, both. Get-Help Set-Location -Full will get you more detailed information on Set-Location , but the basic usage would be PS C:\> Set-Location -Path Q:\MyDir PS Q:\MyDir> By default in PowerShell, CD and CHDIR are alias for Set-Location . (Asad reminded me in the comments that if the path contains spaces, it must be enclosed in quotes.) To go directly to that folder, you can use the Set-Location cmdlet or cd alias: Set-Location "Q:\My Test Folder" Multiple posted answer here, but probably this can help who is newly using PowerShell SO if any space is there in your directory path do not forgot to add double inverted commas "".

Check If A Windows Service Exists And Delete In PowerShell

Answer : You can use WMI or other tools for this since there is no Remove-Service cmdlet until Powershell 6.0 (See Remove-Service doc) For example: $service = Get-WmiObject -Class Win32_Service -Filter "Name='servicename'" $service.delete() Or with the sc.exe tool: sc.exe delete ServiceName Finally, if you do have access to PowerShell 6.0: Remove-Service -Name ServiceName There's no harm in using the right tool for the job, I find running (from Powershell) sc.exe \\server delete "MyService" the most reliable method that does not have many dependencies. If you just want to check service existence: if (Get-Service "My Service" -ErrorAction SilentlyContinue) { "service exists" }

Check If A String Is Not NULL Or EMPTY

Answer : if (-not ([string]::IsNullOrEmpty($version))) { $request += "/" + $version } You can also use ! as an alternative to -not . You don't necessarily have to use the [string]:: prefix. This works in the same way: if ($version) { $request += "/" + $version } A variable that is null or empty string evaluates to false. As in many other programming and scripting languages you can do so by adding ! in front of the condition if (![string]::IsNullOrEmpty($version)) { $request += "/" + $version }

Can LINQ Be Used In PowerShell?

Answer : The problem with your code is that PowerShell cannot decide to which specific delegate type the ScriptBlock instance ( { ... } ) should be cast. So it isn't able to choose a type-concrete delegate instantiation for the generic 2nd parameter of the Where method. And it also does't have syntax to specify a generic parameter explicitly. To resolve this problem, you need to cast the ScriptBlock instance to the right delegate type yourself: $data = 0..10 [System.Linq.Enumerable]::Where($data, [Func[object,bool]]{ param($x) $x -gt 5 }) Why does [Func[object, bool]] work, but [Func[int, bool]] does not? Because your $data is [object[]] , not [int[]] , given that PowerShell creates [object[]] arrays by default; you can, however, construct [int[]] instances explicitly: $intdata = [int[]]$data [System.Linq.Enumerable]::Where($intdata, [Func[int,bool]]{ param($x) $x -gt 5 }) To complement PetSerAl's helpful answer with a broader answer to match the qu...

Bypass Vs Unrestricted Execution Policies

Answer : Per the comments, there should be no particular difference with how these execution policies behave. However Bypass is intended to be used when you are temporarily changing the execution policy during a single run of Powershell.exe , where as Unrestricted is intended to be used if you wish to permanently change the setting for the exeuction policy for one of the system scopes (MachinePolicy, UserPolicy, Process, CurrentUser, LocalMachine). Some examples: You are on a system where you want to change the execution policy to be permanently unrestricted so that any user could run any PowerShell script without issue. You would run: Set-ExecutionPolicy Unrestricted You are on a system where the exeuction policy blocks your script but you want to run it via PowerShell and ignore the execution policy when run. You would run: powershell.exe .\yourscript.ps1 -executionpolicy bypass You run Powershell.exe on a system where the execution policy blocks the exeuction of scri...

Access #text Property Of XMLAttribute In Powershell

Answer : Besides #text , you can also access XmlAttribute 's value via Value property : $attr = $xml.SelectSingleNode("//obj/indexlist/index[@name='DATE']/@value") #print old value $attr.Value #update attribute value $attr.Value = "new value" #print new value $attr.Value Note that Value in $attr.Value is property name of XmlAttribute . It doesn't affected by the fact that the attribute in your XML named value . Don't select the attribute, select the node. The attributes of the node will be represented as properties and can be modified as such: $node = $xml.SelectSingleNode("//obj/indexlist/index[@name='DATE']") $node.value = 'foo' Use a loop if you need to modify several nodes: $nodes = $xml.SelectNodes("//obj/indexlist/index[@name='DATE']") foreach ($node in $nodes) { $node.value = 'foo' }

Azure CLI Vs Powershell?

Answer : Azure CLI is a PowerShell-like-tool available for all platforms. You can use the same commands no matter what platform you use: Windows, Linux or Mac. Now, there are two version Azure CLI. The Azure CLI 1.0 was written with Node.js to achieve cross-platform capabilities, and the new Azure CLI 2.0 is written in Python to offer better cross-platform capabilities. Both are Open Source and available on Github. However, for now, only certain PowerShell cmdlets support use on Linux. Is it targetted for the audience who want to manage Azure IAAS from Linux environment? I think the answer is yes. For a Linux or Mac developer, I think they more likely to use Azure CLI. Both, Azure CLI and the PowerShell package use the REST API of Azure. As one of our Microsoft contacts said: Use whatever you like and you prefer. There are some pros for Azure CLI: Open Source - which has many advantages. It might be developing faster in the future. You can view what is really in...