Powershell 3.0 : Alternative to "Get-Volume" - windows

I'm trying to get various properties for each hdd-volume on the computer.
I was using the cmdlet get-volume and then walking through it via foreach, but that cmdlet does not exist in Windows Server 2008. :(
Does anybody know an alternative?
I just need the drive letter, objectId/guid, free space, total space, and the name of each volume.

The WMI class Win32_Volume has the information you are looking for
Get-WMIObject -Class Win32_Volume | Select DriveLetter,FreeSpace,Capacity,DeviceID,Label
Which a little fancy footwork you can make the drive space properties looking a little more appealing.
Get-WmiObject -Class Win32_Volume |
Select DriveLetter,
#{Label="FreeSpace (In GB)";Expression={$_.Freespace/1gb}},
#{Label="Capacity (In GB)";Expression={$_.Capacity/1gb}},
DeviceID,Label |
Format-Table -AutoSize

Get-Volume is only in Powershell 4.
You can do this tho:
Get-WmiObject Win32_LogicalDisk | Select-Object DeviceID, Size, FreeSpace, VolumeName

Related

How can I uninstall Software Update (Programs and Features\Installed Updates) with Powershell

Is there a simple way of uninstalling Software UPDATES (Service Packs) listed in Programs and Features\Installed Updates with Powershell?
I am not talking about Windows Updates. But a specific software Service Pack that is listed in the same place where the win updates are.
I know how to uninstall Software that is listed in programs and features with PS
But could not figured it out how to remove the Software updates/Service Packs
this command will list all the software
Get-WmiObject -Class Win32_Product | Select-Object -Property Name
or this one
Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate,UninstallString | Format-Table –AutoSize
this will remove the software but i need to remove the software Service Pack( UPDATE) only,
$app = Get-WmiObject -Class Win32_Product | Where-Object {
$_.Name -match "Software Name"
}
$app.Uninstall()
I have seen that post already:
How can I uninstall an application using PowerShell?
I hope that I am clear enough, thanks
long story short is that I need a command that will list or/and remove selected software UPDATES (service packs etc), thanks
any ideas, thanks
Have you tried to search for any similar topics? I found like 9+
What you're looking for is this:
$app = Get-WmiObject -Class Win32_Product | Where-Object {
$_.Name -match "Software Name"
}
$app.Uninstall()
or,
$app = Get-WmiObject -Class Win32_Product -Filter "Name = 'Software Name'"
pulled from: How can I uninstall an application using PowerShell?
Other useful links:
https://redmondmag.com/articles/2019/08/27/powershell-to-uninstall-an-application.aspx
https://www.slashadmin.co.uk/how-to-uninstall-programs-using-powershell/
All with the same conclusions of calling on the method to .uninstall.
for updates:
We can also use DISM for updates:
$SearchUpdates = dism /online /get-packages | findstr "Package_for"
$updates = $SearchUpdates.replace("Package Identity : ", "") | findstr "KBXXXXXX"
#$updates
DISM.exe /Online /Remove-Package /PackageName:$updates /quiet /norestart
Or native powerhsell cmdlets of Get-Package, and uninstall-package.
Made this quick script as a selection for your updates.
$Updates = Get-Package -Name "*Update*" | Select-Object -ExpandProperty Name
for($i=0; $i -lt $Updates.Count; $i++){
write-host "$($i): $($Updates[$i])"
}
$Selection = Read-Host -Prompt "Enter number of updates you would like to uninstall"
$Selection = $Selection -split " "
Foreach($Update in $Updates[$Selection]){
Uninstall-Package -Name $Update -whatif
}
Wusa.exe can also give you similar results if youre familiar with the syntax for it. Run wusa.exe /?. Ex: Wusa /uninstall /KB:KB1234567
Please note, ive only had real success in the past just uninstalling KBs using Wusa. DISM seems to work most of the time, but theres a lot of options available.

How to column print only certain row parts of a list in powershell?

I have tried various ways to format the output from a poweshell command and would like to print only some of the row items in the list as part of a a column in one line.
Perhaps more easy to illustrate:
# I want the output from:
Get-CimInstance Win32_OperatingSystem | select Caption,Version,OSArchitecture,InstallDate | fl
Caption : Microsoft HAL 9000
Version : 6.3.9000
OSArchitecture : 64-bit
InstallDate : 2018-08-16 00:50:01
# To look like this:
Microsoft HAL 9000 (6.3.9000) 64-bit [2018-08-16 00:50:01]
How can this be easily accomplished?
(Coincidentally I want all the rows in this case, but a more general answer may be more useful, if it also include rows that we don't want.)
PowerShell usually returns objects and outputs a string representation of it to the host. You want a custom string format output to the host. You can achieve that in various ways, however the fastest way and my recommendation would be to use the -f operator.
$OS = Get-CimInstance Win32_OperatingSystem
'{0} ({1}) {2} [{3}]' -f $OS.Caption, $OS.Version, $OS.OSArchitecture, $OS.InstallDate
With here-strings use can do the same with multi-line.
$OS = Get-CimInstance Win32_OperatingSystem
#'
My OS is {0} {1})
Architecture --> {2}
Installation Date: [{3}]
'# -f $OS.Caption, $OS.Version, $OS.OSArchitecture, $OS.InstallDate
However, you should work with objects as much as - and as long as it is possible.
I would belive this should work for you:
$temp = (Get-CimInstance Win32_OperatingSystem | Select-Object Caption, Version, OSArchitecture,InstallDate)
The Select-Object makes sure that you get the desired properties. Having a variable with all the details in it, we can concatenate it like this:
"$($temp.Caption) ($($temp.version)) $($temp.OSArchitecture) [$($temp.InstallDate.ToString("yyyy-MM-dd hh:mm:ss"))]"
Simply use Format-Table instead of Format-List. They both support a list of properties you want to see. So if you don't want all columns, list the ones you want.
# 'default' properties in a table
Get-CimInstance Win32_OperatingSystem | ft
# only some properties in a table
Get-CimInstance Win32_OperatingSystem | ft Caption, OSArchitecture
# without table headers
Get-CimInstance Win32_OperatingSystem | ft Caption, OSArchitecture -HideTableHeaders
# all properties in a list (because there are too many for a table)
Get-CimInstance Win32_OperatingSystem | fl *

Register this connection's addresses in DNS in Windows 10

I need a way to programatically (powershell, batch, c#, ...) tell my VPN adapter that it has to register address in DNS.
There seems to be no parameter for this using PowerShell to create the VPN entry:
Powershell.exe -executionpolicy bypass -command Add-VpnConnection -AllUserConnection -Name "ProfileName" -ServerAddress "xyz.com" -TunnelType L2tp -EncryptionLevel Optional -L2tpPsk "password" -AuthenticationMethod MSChapv2 -Force
We had no success doing this by GPO, netsh or c# (DotRas -> VpnEntry.Options.RegisterIPWithDns = true;)
Thanks in advance!
As far as I know there are no way of doing it with the Add-VpnConnection command.
Instead you have to edit the Remote Access Phonebook.
If it is a -alluserconnection VPN the phonebook can be found here:
C:\ProgramData\Microsoft\Network\Connections\Pbk\rasphone.pbk
If it is a per user VPN adapter the phonebook can be found here:
C:\Users\USERNAME\AppData\Roaming\Microsoft\Network\Connections\Pbk\rasphone.pbk
Now use this Powershell command to set the setting:
$RASPhoneBook =
“C:\ProgramData\Microsoft\Network\Connections\Pbk\rasphone.pbk”
(Get-Content $RASPhoneBook) -Replace ‘IpDnsFlags=0’, ‘IpDnsFlags=3’ |
Set-Content $RASPhoneBook
“Register this connection’s addresses in DNS” is now set for your VPN adapter.
Try something like this
Get-NetIPConfiguration | where {$_.some.identifying.vpn.feature -eq 'name'} | Get-NetConnectionProfile | Set-DnsClient -RegisterThisConnectionsAddress:$True
I'd suggest using a different approach for your use case.
Example:
Using PowerShell to Get or Set NetworkAdapterConfiguration-View and Change Network Settings Including DHCP, DNS, IP Address and More (Dynamic AND Static) Step-By-Step
Following commands can be useful to configure the settings:
Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName . | Select-Object -Property [a-z]* -ExcludeProperty IPX*,WINS*
Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "FullDNSRegistrationEnabled=true" –ComputerName . | Get-Member
Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "DomainDNSRegistrationEnabled=true" –ComputerName . | Get-Member

Grab the latest volume using PowerShell

I'm struggling trying to grab the latest volume/Drive using PowerShell
I have a result of a PowerShell look like this
PS C:\Users\me> Get-WMIObject Win32_Volume | select Name
Name
----
C:\
D:\
E:\
\\?\Volume{021a6bbd-0b97-4973-824a-7c635e362f09}\
\\?\Volume{bae1c1d6-59c3-44b1-9360-b7d3101c0e92}\
PS C:\Users\me>
If I want to access just this
E:
How can I filter out to :\ with the highest alphabetical order ?
I've been trying so many options using Select-String, but seems to get worse result.
The ones you want don't start with "\\". The drive letters may be returned in any order, so you need to sort them and take the last one:
Get-WMIObject Win32_Volume | Where-Object {$_.Name -NotLike '\\*'} | select Name | Sort-Object -Property Name | Select-Object -Last 1
Or, if the drive letter is known to be in the range A to Z, then it would be more sensible to use -Like '[A-Z]*' instead of -NotLike '\\*'.
Try something like this
Get-WMIObject Win32_Volume | where {$_.Name -eq "E:\"}
this should give you a list of objects wich you can access like an array. Also there is a lot of useful information here https://technet.microsoft.com/en-gb/library/2007.04.powershell.aspx

Check if Windows path is on locl disk or shared device

I have a java application with many jars. I noticed that running the app from a share takes more time than running it from localdisk.
I would like to create a wrapper script that copies that jars to local disk only if the app was installed on a network path.
Is there a way to find out if a specific folder is on localdisk or network?
you could use wmi for that : check drivetype of the Win32_LogicalDisk class
type 3 => local
type 4 => net drive
an example using powerhsell
gwmi win32_logicaldisk -filter "deviceId='X:'" |select -expand drivetype
or
switch(gwmi win32_logicaldisk -filter "deviceId='X:'" |select -expand drivetype){3{"local"}4{"network"}}

Resources